我应该创建一个通知之前调用激活锁定? [英] Should I call WakeLock before creating a notification?

查看:170
本文介绍了我应该创建一个通知之前调用激活锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加通知一个Android应用程序,并只有模拟器的时刻来测试。
当收到通知时,我的onMessage()在我GCMBaseIntentService子类(GCMIntentService)方法被调用。在这里,我创建一个通知出现。
如果我打开模拟器待机状态时,没有通知被认为是(我不知道这是否会在设备上听说过吗?)。所以,我应该打电话激活锁定在创建通知之前唤醒设备?

I'm adding notifications to an Android app and only have the emulator to test with at the moment. When a notification is received, my onMessage() method in my GCMBaseIntentService subclass (GCMIntentService) is called. From here I create a notification to appear. If I turn the emulator on standby, no notification is seen (I do t know if it would be heard on a device?). So should I be calling WakeLock to wake the device before creating the notification?

感谢

推荐答案

我不知道,如果模拟器在待机是相当于一个锁定装置。如果是,你一定要打电话激活锁定,以便通知,即使设备处于锁定状态出现。

I'm not sure if the emulator being in standby is equivalent to a locked device. If it is, you should definitely call WakeLock in order for the notification to appear even when the device is locked.

下面的示例code:

@Override
protected void onMessage(Context context, Intent intent) {
    // Extract the payload from the message
    Bundle extras = intent.getExtras();
    if (extras != null) {
        String message = (String) extras.get("payload");
        String title = (String) extras.get("title");

        // add a notification to status bar
        NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent myIntent = new Intent(this,MyActivity.class);
        Notification notification = new Notification(R.drawable.coupon_notification, title, System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
        contentView.setImageViewResource(R.id.image, R.drawable.gcm_notification);
        contentView.setTextViewText(R.id.title, title);
        contentView.setTextViewText(R.id.text, message);
        notification.contentView = contentView;
        notification.contentIntent = PendingIntent.getActivity(this.getBaseContext(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        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);
    }
}

当然你需要这个权限添加到您的清单:

Of course you'll need to add this permission to your manifest :

<uses-permission android:name="android.permission.WAKE_LOCK" />

这篇关于我应该创建一个通知之前调用激活锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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