I'm writing an app which speaks and listens to the user. Basically an app which allows placing phone calls without having to look or touch the device (believe it or not I didn't find any in the market!). The point here is that I want:
- to keep running when the screen goes off
- to stop running when the user switches to another activity
And I don't see an easy way to understand this. I've already looked into this: How to stop a background thread when the screen in android device goes off
but the events are being fired in the wrong order (from my logs):
- onPause: screen status UNKNOWN
- onStop: screen status UNKNOWN
screen event: OFF
public class ScreenReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { // do whatever you need to do here wasScreenOn = ScreenStatus.OFF; Log.d(TAG,"screen event OFF"); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { // and do whatever you need to do here wasScreenOn = ScreenStatus.ON; Log.d(TAG,"screen event ON"); } } }