【问题标题】:Problem acquiring wake lock from broadcast receiver从广播接收器获取唤醒锁的问题
【发布时间】:2010-08-13 06:52:26
【问题描述】:

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

在下面的广播接收器中,当 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;
        }
    }
}

【问题讨论】:

    标签: android broadcastreceiver alarm


    【解决方案1】:

    没关系,我想通了 - 我需要在清单中添加唤醒锁定权限:

    使用权限 android:name="android.permission.WAKE_LOCK"

    现在工作正常!

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多