How to make Android WakeLock work?

My WakeLock does not support my device.

In OnCreate() , I have:

 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 ends, how can I make the screen remain visible?

mWakeLock is a field previously declared like this:

 private PowerManager.WakeLock mWakeLock; 

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

+44
android
Jan 11 '10 at 2:08
source share
13 answers

WakeLock usually does not cause reboot problems. There may be other problems in your coding. WakeLock charges the battery strongly if it is not discharged after use.

WakeLock is an inefficient way to save a screen. Instead, use WindowManager to create magic. The next line will be sufficient for Wake Wake. It also requires WakeLock permission. Also this code is more efficient than wakeLock.

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

You do not need to bind Wake Wake manually. This code will allow the Android system to automatically handle the lock. When your application is in Foreground, Wake Wake is held, and the Android system automatically releases the lock.

Try this and post your comment ...

+105
Apr 05 2018-11-11T00:
source share
— -

Do you have the required permission specified in your manifest?

 <uses-permission android:name="android.permission.WAKE_LOCK" /> 
+49
Jan 11 '10 at 7:17
source share

You just need to write this:

  private PowerManager.WakeLock wl; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen"); }//End of onCreate @Override protected void onPause() { super.onPause(); wl.release(); }//End of onPause @Override protected void onResume() { super.onResume(); wl.acquire(); }//End of onResume 

and then add permission to the manifest file

  <uses-permission android:name="android.permission.WAKE_LOCK" /> 

Now your activity will always be awake. You can do other things like w1.release() per your requirement.

+23
Sep 29 '11 at 10:38
source share

Add permission in AndroidManifest.xml as below

 <uses-permission android:name="android.permission.WAKE_LOCK" /> 

preferably BEFORE the <application> ad tags, but AFTER the <manifest> tags, try making the onCreate() method contain only a WakeLock instance.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "My Tag"); } 

and then in your onResume() method

 @Override public void onResume() { mWakeLock.aquire(); } 

and in your onFinish() method

 @Override public void onFinish() { mWakeLock.release(); } 
+7
May 7 '15 at 4:19
source share

To achieve the same program you can use the following

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

or adding the following in the layout will also perform the above task

  android:keepScreenOn="true" 

Details you can get from the following URL http://developer.android.com/training/scheduling/wakelock.html

I used a combination of the following to wake my screen while locking the keypad lock and keep the screen on

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
+6
Jun 09 '14 at 6:38
source share

Turn on screen

First way:

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

The second way:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true"> ... </RelativeLayout> 

Enable CPU:

 <uses-permission android:name="android.permission.WAKE_LOCK" /> 

and

 PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag"); wakeLock.acquire(); 

To release the tracking lock, call wakelock.release() . This frees up your CPU requirement. It is very important to release the lock after your application has finished using it, so as not to drain the battery.

Docs are here .

+4
Jun 07 '17 at 22:23
source share

I have the same problem. I can make the screen stay on, but if I use partial tracking lock and the screen is off, my onFinish function is not called until the screen is turned on.

You can check your wake lock by using mWakeLock.isHeld (), first of all, to make sure you get it. The easiest way is to add this to the code, set a debug breakpoint in it, and then check it.

In my case, I get this, but the partial track lock seems to do nothing. Here is my working code to lock the screen.

 protected void setScreenLock(boolean on){ if(mWakeLock == null){ PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG); } if(on){ mWakeLock.acquire(); }else{ if(mWakeLock.isHeld()){ mWakeLock.release(); } mWakeLock = null; } } 

ADDITION:

Users of Droid Eris and DROID tell me that this does NOT work on their devices, although it works fine on my G1. What device are you testing on? I think it could be an Android bug.

+2
Jan 31 '10 at 2:10
source share

Thanks for this thread. It was difficult for me to execute a timer in my code for 5 minutes to start the event, because my phone, which I set to disconnect / sleep for about 2 minutes. With the above information, it seems I managed to find a job.

  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* Time Lockout after 5 mins */ getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON); Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { Intent i = new Intent(AccountsList.this, AppEntryActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); finish(); return; } }, 300000); /* Time Lockout END */ } 
+2
Aug 13 '13 at 3:21
source share

code snippet from Android developer site

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } 

Recommendations for background jobs

+2
Dec 23 '13 at 8:32
source share
 {PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE); WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyWakeLock"); wakeLock.acquire();} 

use this code and don't forget to enable wakelock in android manifest

+2
Jun 15 '16 at 17:05
source share

Make sure mWakeLock not accidentally cleaned before you are ready to release it. If it is completed, the castle will be released. This can happen, for example, if you set it to null and the garbage collector will subsequently run. This can also happen if you accidentally set a local variable with the same name instead of a method level variable.

I also recommend checking LogCat for entries with the PowerManagerService tag or the tag that you pass to newWakeLock .

+1
Jul 31 2018-11-11T00:
source share

Try using the ACQUIRE_CAUSES_WAKEUP flag when creating a tracking lock. The ON_AFTER_RELEASE flag simply resets the activity timer to keep the screen a little longer.

http://developer.android.com/reference/android/os/PowerManager.html#ACQUIRE_CAUSES_WAKEUP

+1
Aug 03 2018-11-11T00:
source share

Add permission in AndroidManifest.xml:

 <uses-permission android:name="android.permission.WAKE_LOCK" /> 

Then add the code to my.xml:

 android:keepScreenOn="true" 

in this case will never turn off the page! You can read it

0
Apr 03 '18 at 8:25
source share



All Articles