开发者

Android - acquiring a hold lock from a broadcast receiver

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 RepeatingAl开发者_如何学编程armReceiver 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();

    }

}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜