Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing application/screen timeout Android

I have an Android (version 1.5) application which needs to be constantly running when a button is pressed. So, when a Button is pressed I would like the phone to remain on and not want the screen or CPU to time-out.

When another Button is pressed I would like the phone to be back to normal and time-out as per user settings.

like image 990
Tom Avatar asked Jun 13 '09 19:06

Tom


2 Answers

Update: As suggested by Steve Pomeroy, this might be a better way to do it.


You can use a WakeLock that requires the following permission:

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

Here is how you aquire and release a WakeLock:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
// wake locked...
wl.release();

Depending on your requirements you might be able to use a different type of WakeLock.

like image 176
Josef Pfleger Avatar answered Sep 28 '22 05:09

Josef Pfleger


Instead of using a wakelock, you should consider the solution proposed here: Force Screen On

It is much easier to use and doesn't have the potential of accidentally wasting the user's batteries.

like image 40
Steve Pomeroy Avatar answered Sep 28 '22 06:09

Steve Pomeroy