Search code examples
androidandroid-activitybroadcastreceiver

Open activity when screen goes off


I'm trying to make a lock screen app where the app shows up when the screen is awoken. I currently have a broadcast receiver that listens to ACTION_SCREEN_OFF and ACTION_SCREEN_ON and starts the activity. I have tested both of them and it seems that neither one is particularly faster than the other. Even when I listen to ACTION_SCREEN_OFF, the activity doesn't load until a bit after the screen turns on.

Is there a way to actually load the activity when the screen turns off so that it shows up immediately when the screen turns back on? Or at least is there a more efficient way of starting the activity?


Solution

  • There is no more efficient way. When your app launches the Activity from the SCREEN_OFF broadcast, Android creates the Activity then immediately after calls that Activity's onPause() method. Different phones handle it differently however, so things like shadowing, and ghosting are expected just because when you turn the screen on again, the Activity's onResume() is called, so a refresh sometimes happens, depening on device.