My app registers a BroadcastReceiver for each Activity, which detects the SCREEN_OFF state. But if I unregister the receiver in onPause() it never fires. I think it gets unregistered by onPause() before it can receive the SCREEN_OFF broadcast. In this case, where should I unregister the receiver?

Registering the receiver in the manifest should suit the purpose of my app, but I read somewhere that Intent.ACTION_SCREEN_OFF cannot be registered that way.

有帮助吗?

解决方案 2

Such receivers are pretty much useless, because screen-off usually results in Activities being suspended. These are useful for background components like Service, which would like to pause when screen is off.

Screen state can be detected on demand also:

    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    boolean isScreenOn = pm.isScreenOn();

其他提示

Unregister the receiver in the onDestroy() method. So once the activity is destroyed the receiver will be unregistered.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top