I'm implementing an app that listens and takes actions when the user shakes the phone.
So I implemented the following service:
public class ShakeMonitorService extends Service {
...
ShakeDetector shakeDetector;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
shakeDetector= new ShakeDetector(this);
shakeDetector.start();
....
startForeground(MY_ID, myNotification);
return Service.START_STICKY;
}
...
}
and start the service in the MainActivity's onCreate
ContextCompat.startForegroundService(this,
new Intent(this, ShakeMonitorService.class));
In the shakeDetector I listen the sensor events for the accelerometer as described here.
It works correctly on my devices but some users reports that it works until they lock their devices for long time (i.e. 3hours). From this moment the app stops detecting the shake gestures even when using the device (screen on) until they launch the app again.
What is going on? I supposed Android kills or suspends the app and stops the service until it is opened again.
How can I avoid this?
Important note: I don't want to avoid the system to enter in sleep mode, but I want to recover the execution when the system exits the sleep mode.