从广播接收器获取唤醒锁定的问题

时间:2010-08-13 06:52:26

标签: android broadcastreceiver alarm

我有问题。我试图让广播接收器获得唤醒锁定,以便我的闹钟将手机从睡眠模式唤醒。

在下面的广播接收器中,程序在“scpuWakeLock.acquire()”行上遇到“source not found”崩溃;当AlarmReceiver调用类“AlarmAlertWakeLock”时。 知道发生了什么事吗?有没有更好的方法来做我想做的事情?

在一个文件中:

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;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

没关系,我想出来了 - 我需要为清单添加唤醒锁定权限:

uses-permission android:name =“android.permission.WAKE_LOCK”

现在工作正常!

相关问题