73

I was just wondering if it is possible to register a broadcast receiver that detects Screen ON/OFF in the application manifest. The reason why I don't like the programmable method is that it requires the app to be running in order to detect such a thing, while: "Applications with Broadcast Receivers registered in the manifest don’t have to be running when the Intent is broadcast for the receivers to execute" (source: Professional Android 2 Application Development book)

My app is actually a lockscreen app which by using the programmable way needs to be running all the time :S

Is there a way around it?

I'm trying the following in the manifest:

<receiver android:name=".MyBroadCastReciever">
    <intent-filter>
        <action android:name="android.intent.action.SCREEN_OFF"/>
        <action android:name="android.intent.action.SCREEN_ON"/>
    </intent-filter>
</receiver>

and simple MyBroadCastReciever class:

public class MyBroadCastReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.i("Check","Screen went OFF");
            Toast.makeText(context, "screen OFF",Toast.LENGTH_LONG).show();
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.i("Check","Screen went ON");
            Toast.makeText(context, "screen ON",Toast.LENGTH_LONG).show();
        }
    }
}
Yury
  • 20,618
  • 7
  • 58
  • 86
himura
  • 1,555
  • 3
  • 19
  • 30

2 Answers2

91

The two actions for screen on and off are:

android.intent.action.SCREEN_OFF
android.intent.action.SCREEN_ON

But if you register a receiver for these broadcasts in a manifest, then the receiver will not receive these broadcasts.

For this problem, you have to create a long running service, which is registering a local broadcast receiver for these intents. If you do this way, then your app will look for screen off only when your service is running which won't irritate user.

PS: start the service in foreground to make it running longer.

A simple code snippet will be something like this:

IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mScreenStateReceiver, screenStateFilter);

Don't forget to unregister the receiver in the Service's onDestroy:

unregisterReceiver(mScreenStateReceiver);

Just in case for people who are asking why the receiver does not work with the declare broadcasts in manifest for ACTION_SCREEN_ON and ACTION_SCREEN_OFF:

https://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_ON https://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF

You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().

This is a protected intent that can only be sent by the system.

Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31
  • 6
    ok so there's no way around it? this is how lockscreen apps are done? – himura Feb 28 '12 at 07:30
  • 6
    great answer, but why receiver does not work for these broadcasts in manifest? – Bugs Happen Jul 14 '15 at 08:59
  • 3
    If you could listen for ACTION_SCREEN_OFF in the manifest, the OS would have to start a new process for your app if it wasn't running when the broadcast was triggered. The Android designers wanted to prevent this, since it often implies that the device is about to enter a low power state. – Mike Playle Oct 03 '16 at 08:10
  • Please Check this [Screen OFF/ON broadcast listener without service](https://stackoverflow.com/questions/48598200/screen-on-off-broadcast-listener-for-a-widget-on-android-oreo/52853205#52853205) – Ankit Kumar Singh Oct 17 '18 at 13:08
  • i solve with [https://stackoverflow.com/a/59322064/9478814](https://stackoverflow.com/a/59322064/9478814) – civani mahida Dec 13 '19 at 11:54
-1

You need to create a background service to check for it. Then you can set it programmatically.

wildapps
  • 45
  • 4