Android broadcast receiver doesn't receive ACTION_SCREEN_ON

大城市里の小女人 提交于 2019-11-29 05:13:17

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!