0

I wanted a timer and I found that it didn't work when the device went to sleep. I had used AlarmManager but it doesn't work. I also tried service which was still not helpful. Then I tried WakeLock with partial_wake_lock. It worked well at the very beginning but suddenlly failed without changing any code related to the PowerManager or WakeLock. Now it will work for about 1.5 minutes.

The code is:

    private void acquireWakeLock(){
        PowerManager pm=(PowerManager)getSystemService(Context.POWER_SERVICE);
        wl=pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getCanonicalName());
        wl.acquire();
    }
    private void releaseWakeLock(){
        if(wl!=null&&wl.isHeld()){
            wl.release();
            wl=null;
        }
    }

As you can see there's nothing special. And basically I did it like this:

//private members,including a wakelock,a handler and a runnable, etc
onCreate{
acquireWakeLock();
init();
handler.postdelayed(runnable,time);
}
//some functions...
a function  //called in runnable, which does all work before finish() and it calls releaseWakeLock()

I called the function of acquiring in the Activity's onCreate, and released it when all work has been done. When debugging I can see the lock is already held, but when not plugged in it won't do any work after 1.5 minutes.

How can I make it work, or and is there any solution can work well instead this stupid wake lock? ------update----- I have got all permissions I need and as I've mentioned the WakeLock is already held after calling the acquire function and it can be seen in Watch.

水云逸
  • 1
  • 2

1 Answers1

0

May you forgott the permission in Manifest?

<uses-permission android:name="android.permission.WAKE_LOCK" />
Oli
  • 3,496
  • 23
  • 32