androidandroid-sensors

How to get sensor data in service when screen is turned off?


I am trying to create an application that tracks the orientation of the device for a certain amount of time using a service. when the orientation changes, the device makes a sound. This is working perfectly as long as the device is on. As soon as i lock the device or the screen turns off, i dont hear the sounds (which i want to).

My service code is

public class RakatTrackingService extends Service implements SensorEventListener {
private SharedPreferences sharedPref;
private long[] sessionComposition = new long[4];
private String position="none", lastPosition ="none";
private SensorManager sensorManager;
private Sensor accelerometer;
private PowerManager.WakeLock wakeLock;

public RakatTrackingService()
{

}
public RakatTrackingService(long[] sessionComposition) {
    this.sessionComposition = sessionComposition;


}

@Override
public void onCreate() {
    super.onCreate();
    SharedPreferences sharedPref = getSharedPreferences("rakatData",Context.MODE_PRIVATE);

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null)
    {
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        Toast.makeText(this, "sensor registered", Toast.LENGTH_LONG).show();
    }
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onSensorChanged(SensorEvent event) {

    lastPosition = position;
    if(Math.abs(event.values[0]) > Math.abs(event.values[1]) && Math.abs(event.values[0]) > Math.abs(event.values[2]))
    {
        position="horizontal-side";
    }
    if(Math.abs(event.values[1]) > Math.abs(event.values[0]) && Math.abs(event.values[1]) > Math.abs(event.values[2]))
    {
        position="vertical";
    }
    if(Math.abs(event.values[2]) > Math.abs(event.values[0]) && Math.abs(event.values[2]) > Math.abs(event.values[1]))
    {
        position="horizontal";
    }


    System.out.println(position);

    if(!lastPosition.equalsIgnoreCase(position))
    {
        MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
        mp.start();
        mp.setLooping(false);
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    PowerManager mgr = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
    wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    wakeLock.acquire();
    return START_STICKY;

}

@Override
public void onDestroy() {
    super.onDestroy();
    wakeLock.release();
}
}

Please help me resolve this issue.


Solution

  • Simply you can't. Sensor listeners doesn't work then your screen is off. You need the app to be active for using it. You can try to acquire a PARTIAL_WAKE_LOCK or a SCREEN_DIM_WAKE_LOCK