我有一个问题。我正在尝试让广播接收器获取唤醒锁,以便我的闹钟将手机从 sleep 模式唤醒。
在下面的广播接收器中,当 AlarmReceiver 调用类“AlarmAlertWakeLock”时,程序在“sCpuWakeLock.acquire();”行崩溃并显示“找不到源”。 知道发生了什么事吗?有没有更好的方法来完成我想做的事情?
在一个文件中:
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;
}
}
}
最佳答案
没关系,我想通了 - 我需要向 list 添加唤醒锁权限:
使用权限 android:name="android.permission.WAKE_LOCK"
现在工作正常!
关于android - 从广播接收器获取唤醒锁时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3474567/