我有一个问题。我试图做一个广播接收器获得一个唤醒锁,所以我的闹钟将唤醒手机从休眠模式。
在下面的广播接收器,程序崩溃与源未找到上线sCpuWakeLock.acquire();上课的时候AlarmAlertWakeLock被称为AlarmReceiver。 任何想法是怎么回事?有没有更好的办法做我想要做什么?
在一个文件中:
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
公共类AlarmReceiver扩展的BroadcastReceiver {
@覆盖
公共无效的onReceive(最终上下文的背景下,意图意图){
AlarmAlertWakeLock.acquireCpuWakeLock(上下文);
}
}
在一个单独的文件:
进口android.content.Context;
进口android.os.PowerManager;
公共类AlarmAlertWakeLock {
私有静态PowerManager.WakeLock sCpuWakeLock;
静态无效acquireCpuWakeLock(上下文的背景下){
如果(sCpuWakeLock!= NULL){
返回;
}
电源管理器PM =(电源管理器)context.getSystemService(Context.POWER_SERVICE);
sCpuWakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP,okTag);
sCpuWakeLock.acquire();
}
静态无效releaseCpuLock(){
如果(sCpuWakeLock!= NULL){
sCpuWakeLock.release();
sCpuWakeLock = NULL;
}
}
}
解决方案
没关系,我理解了它 - 我需要唤醒锁权限添加到清单:
使用-权限的Android:名称=android.permission.WAKE_LOCK
现在正常工作!
I have a problem. I am trying to make a broadcast receiver acquire a wake lock so my alarm will wake the phone from sleep mode.
In the broadcast receiver below, the program crashes with "source not found" on line "sCpuWakeLock.acquire(); when the class "AlarmAlertWakeLock" is called by AlarmReceiver. Any idea what's going on? Is there a better way to do what I'm trying to do?
In one file:
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);
}
}
In a separate file:
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;
}
}
}
解决方案
Never mind, I figured it out - I needed to add wake lock permission to the manifest:
uses-permission android:name="android.permission.WAKE_LOCK"
Works fine now!