开发者

Problem acquiring wake lock from broadcast receiver

开发者 https://www.devze.com 2023-01-11 22:28 出处:网络
I have a problem. I am trying to make a broadcast receiver acquire a wake lock so my alarmwill wake the phone from sleep mode.

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消