0

I have got an alarm manager and I am working with some tasks in background with alarm managers. I use wakelock for waking CPU in order to finish my background work which causes the error below. I have searched and found that a type of wakelock must be specified and I shouldn't use ACQUIRE_CAUSES_WAKEUP. What should I use instead?

My code:

private static PowerManager.WakeLock wakeLock;
public static void acquirWakeLock(){
    if(wakeLock!=null){
        wakeLock.release();
    }

    PowerManager pm=(PowerManager) KITILApplication.getappContext().getSystemService(Context.POWER_SERVICE);
//Error is below line  
wakeLock=pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE,"aqs_wake_lock");
    wakeLock.acquire();
}
public static void releaseWakeLock(){
    if(wakeLock!=null)
        wakeLock.release();
    wakeLock=null;
}

My Error:

Caused by: java.lang.IllegalArgumentException at android.os.PowerManager$WakeLock.<init>
IvBaranov
  • 540
  • 2
  • 10
  • 24
Fahim
  • 384
  • 5
  • 20

2 Answers2

1

checkout the official documentation : https://developer.android.com/reference/android/os/PowerManager.html

Salah Hammouda
  • 303
  • 2
  • 19
0

PowerManager.ACQUIRE_CAUSES_WAKEUP is not a valid wake lock level. Valid parameters according to validateWakeLockParameters() method are:

PARTIAL_WAKE_LOCK
SCREEN_DIM_WAKE_LOCK
SCREEN_BRIGHT_WAKE_LOCK
FULL_WAKE_LOCK
PROXIMITY_SCREEN_OFF_WAKE_LOCK
DOZE_WAKE_LOCK
DRAW_WAKE_LOCK

So, instead of ACQUIRE_CAUSES_WAKEUP exactly one wake lock is intended to be used, for example:

newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "aqs_wake_lock");

See also:

https://developer.android.com/reference/android/os/PowerManager.html#newWakeLock

How to get an Android WakeLock to work?

IvBaranov
  • 540
  • 2
  • 10
  • 24