سؤال

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