6

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.

Mauker
  • 11,237
  • 7
  • 58
  • 76
Tom
  • 7,316
  • 9
  • 35
  • 35

2 Answers2

3

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.

Community
  • 1
  • 1
Josef Pfleger
  • 74,165
  • 16
  • 97
  • 99
  • I did think of that solution however I can only release the wake lock where it was locally created, not in the "Off" button which is where it needs to be released. Ideas? – Tom Jun 13 '09 at 20:24
  • 2
    Create a service. In the service's onCreate(), grab the WakeLock, held in a private data member. In the service's onDestroy(), release the WakeLock. In the activity, when you need the screen/CPU to stay on, call startService(). Sometime later, when you no longer need the screen/CPU to stay on, call stopService(). Now, the WakeLock will be held so long as the service is running, no matter what happens to your activity. Moreover, the service could also watch for battery events, to perhaps release the WakeLock anyway once battery life gets low. – CommonsWare Jun 13 '09 at 21:18
  • I keep getting "wl can not be resolved" on the .release method: public class WakeLockService extends Service { @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } public void onCreate() { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag"); wl.acquire(); } public void onDestroy() { wl.release(); } } – Tom Jun 13 '09 at 22:01
  • Oh man... Services are way more complicated than that. Check out the guide: http://developer.android.com/guide/developing/tools/aidl.html – Jeremy Logan Jun 13 '09 at 22:31
  • See my note below. Wakelocks can be dangerous and should only be used when really needed. – Steve Pomeroy Dec 16 '10 at 16:05
  • 1
    I think here you *do* need the wake lock though, as it's to be controlled when pressing a button within an Activity, rather than for an entire Activity's (foreground) lifespan. – Christopher Orr Dec 17 '10 at 20:10
2

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.

Community
  • 1
  • 1
Steve Pomeroy
  • 10,071
  • 6
  • 34
  • 37