WakefulBroadcastReceiver 没有唤醒屏幕
Posted
技术标签:
【中文标题】WakefulBroadcastReceiver 没有唤醒屏幕【英文标题】:WakefulBroadcastReceiver did not wake up the screen 【发布时间】:2014-08-25 05:03:39 【问题描述】:我正在使用 GCM 来获取通知。 我应该在收到通知时唤醒屏幕设备。
我该怎么做?
在测试应用时,设备只播放通知声音和振动,不播放唤醒屏幕。
这是 WakefullBroadcastReceiver 的 sn-p:
@Override
public void onReceive(Context context, Intent intent)
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
和 IntentService:
public class GCMNotificationIntentService extends IntentService
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
//NotificationCompat.Builder builder;
public GCMNotificationIntentService()
super("GcmIntentService");
public static final String LOG_TAG = GCMNotificationIntentService.class.getSimpleName();
@Override
protected void onHandleIntent(Intent intent)
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty())
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType))
sendNotification("Send error: " + extras.toString());
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType))
sendNotification("Deleted messages on server: "
+ extras.toString());
else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType))
for (int i = 0; i < 3; i++)
Log.d(LOG_TAG,
"Working... " + (i + 1) + "/5 @ "
+ SystemClock.elapsedRealtime());
try
Thread.sleep(500);
catch (InterruptedException e)
Log.d(LOG_TAG, "Completed work @ " + SystemClock.elapsedRealtime());
sendNotification("Notification: "
+ extras.get(Config.MESSAGE_KEY));
Log.d(LOG_TAG, "Received: " + extras.toString());
GcmBroadcastReceiver.completeWakefulIntent(intent);
private void sendNotification(String msg)
Log.d(LOG_TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyActivity.class), 0);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
long[] vibrate = 0,100,200,300;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("TITLE")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setDefaults(NotificationCompat.DEFAULT_LIGHTS)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setAutoCancel(true)
.setTicker("New Message");
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(LOG_TAG, "Notification sent successfully.");
非常感谢。
【问题讨论】:
【参考方案1】:必须在 GCMNotificationIntentService onHandleIntent 内部运行:
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "wakeLock");
wakeLock.acquire();
几秒钟后关闭屏幕应该运行
wakeLock.release();
【讨论】:
以上是关于WakefulBroadcastReceiver 没有唤醒屏幕的主要内容,如果未能解决你的问题,请参考以下文章