Search code examples
androidandroid-1.5-cupcake

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.


Solution

  • 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.