Tom
Tom

Reputation: 7426

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.

Upvotes: 6

Views: 4087

Answers (2)

Josef Pfleger
Josef Pfleger

Reputation: 74527

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.

Upvotes: 3

Steve Pomeroy
Steve Pomeroy

Reputation: 10129

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.

Upvotes: 2

Related Questions