Let’s say an Android phone is in sleep mode and upon receiving push notifications we want the phone to wake up (come out of sleep mode) and act upon (display etc.) the notification. In this case, upon receiving the notification which invokes the notification handler, we can wake up the processor by acquiring the Wakeup lock, act on the notification and then release the lock. 


The following code snippet shows the same:

package com.codercrunch.picwords.gcm;

import android.content.Context;
import android.os.PowerManager;

public abstract class WakeLocker {

// handle to lock
private static PowerManager.WakeLock wakeLock;

/**
 * Acquire the Lock
 *
 * @param context
 */
public static void acquire(Context context) {

     // check if null, release it
    if (wakeLock != null)
       wakeLock.release();

     // get handle to power manager a create new wake lock
     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
     wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP 
     | PowerManager.ON_AFTER_RELEASE, "WakeLock");

     // acquire     
     wakeLock.acquire();
}

/**
 * Release the Lock
 */
public static void release() {
 
   // check and release
   if (wakeLock != null)
       wakeLock.release();
       wakeLock = null;
    }
}

In order to get the lock, make sure you declare the following permission in the Android Manifest.

<!– Keeps the processor from sleeping when a message is received. –>
 <uses-permission android:name=”android.permission.WAKE_LOCK” />