BroadcastReceiver的不叫,当屏幕锁定的Andr​​oid [英] BroadcastReceiver not called when screen locked in Android

查看:130
本文介绍了BroadcastReceiver的不叫,当屏幕锁定的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,当通知到达时,的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.

这篇关于BroadcastReceiver的不叫,当屏幕锁定的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆