Android: broadcast receiver to turn on and off the screen

I'm just wondering if it's possible to register a broadcast receiver that detects on / off screens in the application manifest. The reason I don’t like the programmable method is because the application must work to detect such a thing, while: "Applications with broadcast receivers registered in the manifest should not start when Intent is broadcast to receivers to execute "(source: Professional Android 2 Application Development book)

My application is actually a lockscreen application that should work all the time using a programmable method: S

Is there any way?

I try 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 a simple class MyBroadCastReciever:

 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(); } } } 
+48
android screen broadcastreceiver
Feb 28 '12 at 7:21
source share
2 answers

Two steps to turn the screen on and off:

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

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

For this problem, you need to create a long service that registers a local broadcast receiver for these purposes. If you do this, your application will only search for the screen if your service is running, which will not annoy the user.

PS: start the service in the foreground so that it works longer.

A simple piece of code would be something like this:

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

Do not forget to unregister the recipient in the onDestroy service:

 unregisterReceiver(mScreenStateReceiver); 

Just in case, for people who ask why the recipient does not work with the broadcast declaration in the 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 get this through the components declared in the manifest, only by explicitly registering for it using Context.registerReceiver ().

This is a secure design that can be sent by the system.

+63
Feb 28 '12 at 7:29
source share

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

0
Jul 02 '14 at 3:07
source share



All Articles