Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I call WakeLock before creating a notification?

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?

Thanks

like image 630
Darren Avatar asked Apr 23 '13 12:04

Darren


People also ask

How do you use Wakelock?

To release the wake lock, call wakelock. release() . This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.

What is Wakelock on my Android phone?

A wakelock is a powerful concept in Android that allows the developer to modify the default power state of their device. The danger of using a wakelock in an application is that it will reduce the battery life of a device.

How do you release a Wakelock?

you can release it in onDestroy() of last activity that gets popped by activitymanager.

What is CPU Wakelock?

So, what exactly are these? Wakelocks are power-managing software mechanisms, which make sure that your Android device doesn't go into deep sleep (which is the state that you should strive for), because a given app needs to use your system resources.


1 Answers

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.

Here's sample 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" />
like image 80
Eran Avatar answered Sep 18 '22 11:09

Eran