小编Bil*_*lie的帖子

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

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

在下面的广播接收器中,程序在"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);

    }    
}
Run Code Online (Sandbox Code Playgroud)

在单独的文件中:

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; …
Run Code Online (Sandbox Code Playgroud)

android broadcastreceiver alarm

7
推荐指数
1
解决办法
4728
查看次数

标签 统计

alarm ×1

android ×1

broadcastreceiver ×1