I tried to register the receiver in my service with the following code:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
ScreenReceiver SR =
Read this tutorial Handling Screen OFF and Screen ON Intents, it could help you.
Maybe you should check which intent is coming inside your onReceive()
method:
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)
{
...
}
Also, have you tried putting a breakpoint in there?
Other advice, instead of using System.out.println
use the API native log which would be in your case
Log.i("ScreenReceiver""RECEIVED");
And you can read the log in the logcat.
It's work for me on Android 4.0.4
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
//do something you need when broadcast received
}
};
IntentFilter filter = new IntentFilter()
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
context.registerReceiver(receiver, filter);
As for register receiver with action Intent.ACTION_SCREEN_ON and Intent.ACTION_SCREEN_OFF in Manifest.xml it don't help because Android code in PowerManagerService.java following:
...
mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
...