Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an Android WakeLock to work?

Tags:

android

My WakeLock isn't keeping my device awake.

In OnCreate() I've got:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "My Tag"); mWakeLock.acquire(); 

then:

new CountDownTimer(1320000, 200) {      public void onTick(long millisUntilFinished) {         // I update a progress bar here.                                              }      public void onFinish() {         // I finish updating the progress bar.         mWakeLock.release();     } }.start(); 

The screen turns off before the timer finishes, how can I make the screen stay visible?

mWakeLock is a field previously declared like so:

private PowerManager.WakeLock mWakeLock; 

My device uses Android 1.6. I would really appreciate any help to resolve this.

like image 830
Curyous Avatar asked Jan 11 '10 02:01

Curyous


People also ask

How do I use Wakelock on Android?

To release the wake lock, call wakelock. release() . This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.

What is Wakelock permission Android?

A wake lock is a mechanism to indicate that your application needs to have the device stay on. Any application using a WakeLock must request the android. permission. WAKE_LOCK permission in an <uses-permission> element of the application's manifest.

What does it mean when an app holds a Wakelock?

A wakelock, in layman's terms, is just a way for an app to keep the CPU/screen/other things awake when the phone is idle in order to perform a specific background task.

How do you use a Wakelock blocker?

Click on the wakelock you want to block, press the "Block" button and then set for how long is the wakelock gonna be blocked!


2 Answers

WakeLock doesn't usually cause Reboot problems. There may be some other issues in your coding. WakeLock hogs battery heavily, if not released after usage.

WakeLock is an Inefficient way of keeping the screen on. Instead use the WindowManager to do the magic. The following one line will suffice the WakeLock. The WakeLock Permission is also needed for this to work. Also this code is more efficient than the wakeLock.

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

You need not relase the WakeLock Manually. This code will allow the Android System to handle the Lock Automatically. When your application is in the Foreground then WakeLock is held and else android System releases the Lock automatically.

Try this and post your comment...

like image 183
Anoop Chandrika HarisudhanNair Avatar answered Sep 19 '22 21:09

Anoop Chandrika HarisudhanNair


Do you have the required permission set in your Manifest?

<uses-permission android:name="android.permission.WAKE_LOCK" /> 
like image 30
wf. Avatar answered Sep 22 '22 21:09

wf.