I tried to register the receiver in my service with the following code:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
ScreenReceiver SR = new ScreenReceiver();
registerReceiver(SR, filter);
and this is my broadcast receiver:
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("RECEIVED");
}
}
I have even declared it in the manifest but without any filters:
<receiver android:name=".ScreenReceiver" android:enabled="true" />
But whatever I do, I can't seem to be able to receive the intent when I turn my screen on.
Is this intent disabled in android ICS or JB or am i missing something?
Any help would be appreciated!
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);
...
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.