tags:

views:

20

answers:

2

HI, I am trying to aquire a wake lock however when I call PowerManager.newWakeLock() I receive a IlleglArgumentException.

Here is the code I use to get the

private void setWakeLock(Context context)
{
    PowerManager pm = (PowerManager) context
    .getSystemService(Context.POWER_SERVICE);
    SoundAlarmActivity.WakeLock = pm.newWakeLock(
    PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE, "Alarm");
    SoundAlarmActivity.WakeLock.acquire();
}

And here is the logcat:

09-28 12:53:09.704: ERROR/AndroidRuntime(702): java.lang.IllegalArgumentException
09-28 12:53:09.704: ERROR/AndroidRuntime(702):     at android.os.PowerManager$WakeLock.<init>(PowerManager.java:223)
09-28 12:53:09.704: ERROR/AndroidRuntime(702):     at android.os.PowerManager.newWakeLock(PowerManager.java:365)
+1  A: 

Try this:

private void setWakeLock(Context context)
{
    PowerManager pm = (PowerManager) context
    .getSystemService(Context.POWER_SERVICE);
    SoundAlarmActivity.WakeLock = pm.newWakeLock(
    PowerManager.FULL_WAKE_LOCK |
        PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE, "BusSnoozeAlarm");
    SoundAlarmActivity.WakeLock.acquire();
}

PowerManager.ACQUIRE_CAUSES_WAKEUP does not work with partial wake locks so you have to make sure you are using a FULL_WAKE_LOCK while defining the type of Wake Lock you want.

Donal Rafferty
+2  A: 

You should define what "kind" of wake lock you want. The two flags you are giving are just additions to the type :

Flag Value                  CPU Screen  Keyboard
PARTIAL_WAKE_LOCK           On* Off Off
SCREEN_DIM_WAKE_LOCK    On  Dim Off
SCREEN_BRIGHT_WAKE_LOCK On  Bright  Off
FULL_WAKE_LOCK          On  Bright  Bright
Matthieu
Be aware that ACQUIRE_CAUSES_WAKEUP will not work with a PARTIAL_WAKE_LOCK though.
Donal Rafferty