I have an Android service that adds a system overlay view (TYPE_SYSTEM_ALERT) in the onCreate method
WindowManager.addView(mOverlayView, mLayoutParams);
and removes it in the onDestroy method using
WindowManager.removeView(mOverlayView);
When the view is double tapped, it's removed from the screen by closing the service using
stopSelf();
The overlay view keeps the screen on by using
android:keepScreenOn="true"
Now, after I press the Power button to put the phone to sleep, then press it again to wake it up, after unlocking, the overlay view is still open and over the screen.
How can I make the service stop itself when/after I press the Power button and the phone goes to sleep? I don't want the overlay view to be visible on screen after waking up and unlocking the phone.
Watch for ACTION_POWER_OFF
and/or ACTION_POWER_ON
broadcasts. Note that AFAIK you can only register for those from your service via registerReceiver()
, not via your manifest. Since your service is already running, this shouldn't be an issue. When you get the broadcast, stop the service.