How to get an Android WakeLock to work?
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.
Asked by: Sydney884 | Posted: 20-01-2022
Answer 1
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...
Answered by: Agata851 | Posted: 21-02-2022Answer 2
Do you have the required permission set in your Manifest?
<uses-permission android:name="android.permission.WAKE_LOCK" />
Answered by: Aston356 | Posted: 21-02-2022
Answer 3
You just have 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 in 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()
as per your requirement.
Answer 4
Keep the Screen On
First way:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
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>
Keep the CPU On:
<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 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.
Docs here.
Answered by: Blake520 | Posted: 21-02-2022Answer 5
Add permission in AndroidManifest.xml
, as given below
<uses-permission android:name="android.permission.WAKE_LOCK" />
preferably BEFORE your <application>
declaration tags but AFTER the <manifest>
tags, afterwards, try making your onCreate()
method contain only the WakeLock instantiation.
@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 place
@Override
public void onResume() {
mWakeLock.aquire();
}
and in your onFinish()
method place
@Override
public void onFinish() {
mWakeLock.release();
}
Answered by: Daniel605 | Posted: 21-02-2022
Answer 6
To achieve the same programmatic you can use following
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
or adding following in layout also will perform the above task
android:keepScreenOn="true"
The details you can get from following url http://developer.android.com/training/scheduling/wakelock.html
I have used combination of following to wake my screen when keyguard locked and keep my 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);
Answered by: Miranda863 | Posted: 21-02-2022
Answer 7
I am having a similar problem. I can get the screen to stay on, but if I use a partial wake lock and the screen is turned off, my onFinish function isn't called until the screen is turned on.
You can check your wake lock using mWakeLock.isHeld(), first of all, to make sure you're getting it. Easiest way is to add that to the code, set a breakpoint on it in the debugger, and then check it.
In my case, I'm getting it, but the partial wake lock doesn't seem to be doing anything. Here's my working code for the screen dim lock.
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;
}
}
ADDENDUM:
Droid Eris and DROID users are reporting to me that this DOES NOT work on their devices, though it works fine on my G1. What device are you testing on? I think this may be an Android bug.
Answered by: Melissa503 | Posted: 21-02-2022Answer 8
Try using the ACQUIRE_CAUSES_WAKEUP flag when you create the wake lock. The ON_AFTER_RELEASE flag just resets the activity timer to keep the screen on a bit longer.
http://developer.android.com/reference/android/os/PowerManager.html#ACQUIRE_CAUSES_WAKEUP
Answered by: Brooke785 | Posted: 21-02-2022Answer 9
Thank you for this thread. I've been having a hard time implementing a Timer in my code for 5 minutes to run an activity, because my phone I have set to screen off/sleep around 2 minutes. With the above information it appears I have been able to get the work around.
@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 */
}
Answered by: Wilson183 | Posted: 21-02-2022
Answer 10
sample code snippet from android developers 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);
}
Best Practices for Background Jobs
Answered by: Emily872 | Posted: 21-02-2022Answer 11
{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 permit wakelock in android manifest
Answered by: Lana907 | Posted: 21-02-2022Answer 12
Make sure that mWakeLock
isn't accidentally cleaned up before you're ready to release it. If it's finalized, the lock is released. This can happen, for example, if you set it to null and the garbage collector subsequently runs. It can also happen if you accidentally set a local variable of the same name instead of the method-level variable.
I'd also recommend checking LogCat for entries that have the PowerManagerService tag or the tag that you pass to newWakeLock
.
Answer 13
Add permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Then add code in my.xml:
android:keepScreenOn="true"
in this case will never turn off the page! You can read more this
Answered by: Dainton825 | Posted: 21-02-2022Answer 14
If your use-case is making the screen (activity) ON while doing an action, you shouldn't use WakeLock.
The best way to do that is by enabling the flag FLAG_KEEP_SCREEN_ON.
You can do it programmatically:
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
Or in your application's layout XML file, by using the android:keepScreenOn attribute:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
If that's not the case, check these alternative solutions according to your use-case Alternatives to using wake locks
Answered by: Sophia643 | Posted: 21-02-2022Similar questions
Does the Android OS release a wakelock if the app or service holding it is killed?
I have question about wakelock. In cases shown below, does android OS release wakelock (PARTIAL_WAKE_LOCK if you need to specify) to prevent wakelock was left acquired and wasting battery until turning power off (not sleep).
Case 1-a:
App has acquired wakelock (w/o timeout option) in one of its threads (please think it is reasonable in this case) and it was designed to release wakelock whe...
java - Could WakeLock cause phone reboot?
I have an app need to continuously using WiFi and accelerometer sensor to collect data, so I need to use WakeLock method to keep these sensors active. But now I meet the problem that the phone sometimes randomly reboot after running several hours. I don't know whether this causes by my app or the phone's firmware. Does anybody have some ideas about it. Actually, only one thing I think special in my app is ...
android - Do I need to obtain a WakeLock if I don't create threads?
I'm creating a Service that will be set up to start using the AlarmManager (using ELAPSED_REALTIME_WAKEUP). I do all my processing inside the onStartCommand method mainly because:
It doesn't take long (read small file from disk, maybe push notifications via NotificationManager)
I can easily set it to start in a separate process if I nee...
android - Using WakeLock to Keep A Stream Playing
I have a MediaPlayer running in a Service that's playing audio from a URL (streaming). As of now it appears to work well, and even continues playing when I put the phone in standby.
I currently do not acquire a wakelock. My question is:
Is it actually necessary to acquire a wakelock in my situation?
If it is necessary, what type of wakelock should I acquire?
And yes, this ...
wakelock - Turn off screen on Android
I am trying to turn on and off the display after a certain action happens (Lets just worry about turning the screen off for now). From what I understand from wake lock, this is what I have:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
When I read other posts on stac...
android - Can I get rid of WakeLock?
My app has a couple of services running in the background. They use: a long lived TCP connection, GPS, Accelerometer and android.os.CountDownTimer.
I already know the Accelerometer and CountDownTimer require a partial WakeLock to work when my phone goes to sleep (Android 2.2), GPS doesn't require a WakeLock, not sure about TCP sockets?
CountDownTimer hopefully can be replaced by java.util.Timer which I thin...
android - WakeLock doesn't seem to be working as planned
I'm trying to turn the screen on when the app sends a notification. Currently when I acquire a WakeLock it flashes on and off very quickly, almost un-noticeably. As far as I can tell I have my flags set correctly. AQUIRE_CAUSES_WAKEUP to turn the screen on and ON_AFTER_RELEASE to (poke the user activity timer) leave it on once I release it - otherwise, I understa...
android - Does a Thread running from a service with wakelock require wakelock?
I want to run a thread to do some work in the background (from a service that acquired a wakelock). after my thread finish work then it will stop the service.
My concerns are :
1- Do i need to require a wakelock in the thread that was started in the service?
2- Can the system kill my thread while leaving my service running?
3- if 2 is the case how could i stop my service (can i give th...
android - How to turn wakelock off?
I'm tryng to turn proximitywakelock on when portrait and off when landscape using this code, but it always stays active:
//in onCreate() of my Activity
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mProximityWakeLock = pm.newWakeLock(32, ""); // proximity_wake_lock=32
mProximityWakeLock.setReferenceCounted(false);
if (getResources().getConfiguration().o...
android - WakeLock turns screen on only when charging
I wrote a simple countdown timer app and I'm using the code below to turn the screen on, vibrate and play an alarm sound whenever a countdown is finished:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
RingTone r = RingtoneManager.getRingtone(mContext, notification);
WakeLock wl = pm.newWakeLoc...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android