Problem Associated with Locking Tracking from a Broadcast Receiver

I have a problem. I am trying to get the broadcast receiver to get a tracking lock, so my alarm clock will wake the phone from sleep mode.

In the broadcast receiver below, the program crashes "source not found" in the line "sCpuWakeLock.acquire () when the AlarmAlertWakeLock class is called by AlarmReceiver. Any idea what happens? 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; } } } 
+7
android broadcastreceiver alarm
source share
1 answer

Nothing, I realized this - I needed to add permission to block tracking to the manifest:

use-permission android: name = "android.permission.WAKE_LOCK"

Now works great!

+4
source share

All Articles