从广播接收器获取唤醒锁时的问题
我有个问题。 我正在尝试让广播接收器获取唤醒锁,以便我的闹钟将手机从睡眠模式中唤醒。
在下面的广播接收器中,当“AlarmAlertWakeLock”类被AlarmReceiver调用时,程序崩溃,并在“sCpuWakeLock.acquire()”行中找不到“source not found”;有没有更好的方法来做我想要做什么?
在一个文件中:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
AlarmAlertWakeLock.acquireCpuWakeLock(context);
}
}
在一个单独的文件中:
import android.content.Context;
import android.os.PowerManager;
public class AlarmAlertWakeLock {
private static PowerManager.WakeLock sCpuWakeLock;
static void acquireCpuWakeLock(Context context) {
if (sCpuWakeLock != null) {
return;
}
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
sCpuWakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP,"okTag");
sCpuWakeLock.acquire();
}
static void releaseCpuLock() {
if (sCpuWakeLock != null) {
sCpuWakeLock.release();
sCpuWakeLock = null;
}
}
}
没关系,我想清楚了 - 我需要为清单添加唤醒锁定权限:
使用权限android:name =“android.permission.WAKE_LOCK”
现在正常工作!
链接地址: http://www.djcxy.com/p/47329.html