BroadcastReceiver的不叫,当屏幕锁定的Andr​​oid不叫、屏幕、BroadcastReceiver、Andr

2023-09-07 12:37:09 作者:Weak

在我的应用程序,当通知到达时,的BroadcastReceiver 不叫,如果屏幕被锁定。 但是,当屏幕被锁定时,的BroadcastReceiver 被调用,显示通知。

In my Application, when a notification arrives, the BroadcastReceiver is not called if the screen is locked. But when screen is unlocked, the BroadcastReceiver is called and displays the notification.

我也把下面的权限在我的清单:

I also put the following permission in my manifest:

android.permission.WAKE_LOCK

但仍然没有工作。

But still not working.

推荐答案

这里的code,它为我的作品:

Here's the code that works for me:

NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(...);
...
mManager.notify(0, notification);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);

请确保你从你的服务器 delay_while_idle = 0 发送通知(这是默认值),或通知将不会被GCM发送至您的设备醒来。

Make sure that you send the notification from your server with delay_while_idle=0 (that's the default value), or the notification won't be sent by GCM until your device wakes up.