views:

124

answers:

2

Hi. I am trying to acquire a wake lock in a broadcast receiver so that my alarm clock application can wake the phone from sleep. It crashes at the following line in the code below:

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");

Any ideas what's going on? Is there a better way to do this? Thanks!

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.PowerManager;

public class RepeatingAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) { 
         AlarmAlertWakeLock.acquireCpuWakeLock(context);
         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
         wl.acquire();

    }

}
A: 

You use the context of the receive method to get the power manager and i think that is the context of the sender of the intent so use the context of your app that should work.

yaourt
A: 

Make sure you have the WAKE_LOCK permission (check your AndroidManifest.xml).

adamk
This worked, thanks!!!
Billie