Reputation: 97
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.
Upvotes: 4
Views: 1715
Reputation: 11
You can use a foreground service with a notification, however some sensors will get disabled after a while when the screen is off.
Upvotes: 1
Reputation: 97
So after some digging on the inet, i've come to a conclusion that the service is live but the accelerometer gets disabled when the screen is turned off. This property turns out to be device and brand specific. I am now using a screen dim wakelock to keep the screen on. As for the lock feature, im turning to notifying the user not to lock the screen while using the app. Its doable considering the nature of the app im making.
Upvotes: 2
Reputation: 3393
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
Upvotes: 2