Welcome Android Developpers

Welcome Android Developpers
My name is Mathias Séguy, I am an Android Expert and Trainer and created the Android2EE company.

For full information, it’s here : Android2EE's Training
You can meet me on Google+, follow me on Twitter or on LinkedIn

Wednesday, November 16, 2011

How to disable sleep mode ?o?

Bandeau Conference
Hello,
It's quite easy,
You should use the PowerManager object to retrieve PowerService, then get the PowerManager.WakeLock object and ask for it. So in your Activity class declare the following attribute:


/**
 * The WaveLock object
 */
private PowerManager.WakeLock dimWaveLock;
/**
 * The tag to obtain the wave lock
 */
private static final String WAVE_LOCK_TAG = "MyLightLockTag";

In your onResume method, initialize and acquire the waveLock
// The phone should not go in sleep mode:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
dimWaveLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCKWAVE_LOCK_TAG);
dimWaveLock.acquire();

And in your onPause method, release the object:
//and also don't forget to accept the phone go back in sleep mode
waveLock.release();

And if you want to keep the brigthness of your application, do the same with the SCREEN_BRIGHT_WAKE_LOCK parameter :
/**
 * The WaveLock object
 */
private PowerManager.WakeLock dimWaveLock;
/**
 * The WaveLock object
 */
private PowerManager.WakeLock screenWaveLock;
/**
 * The tag to obtain the wave lock
 */
private static final String WAVE_LOCK_TAG = "MyLightLockTag";
....
/*
 * (non-Javadoc)
 *
 * @see android.app.Activity#onResume()
 */
@Override
protected void onResume() {
// The phone should not go in sleep mode:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
dimWaveLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, WAVE_LOCK_TAG);
screenWaveLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, WAVE_LOCK_TAG);
dimWaveLock.acquire();
screenWaveLock.acquire();
}
...
/*
 * (non-Javadoc)
 *
 * @see android.app.Activity#onPause()
 */
@Override
protected void onPause() {
...
// and also don't forget to accept the phone go back in sleep mode
dimWaveLock.release();
screenWaveLock.release();
}


So, Thanks who?
Thanks, Android2ee, the Android Programming Ebooks :o)

Mathias Séguy
mathias.seguy.it@gmail.com
Auteur Android2EE
Ebooks to learn Android Programming.
AndroidEvolution


Retrouvez moi sur Google+
Suivez moi sur Twitter
Rejoignez mon réseau LinkedIn ou Viadeo

5 comments:

  1. hi there, ive tried to use your method, but my app closes without any reason when i put the line: "dimWaveLock.acquire();" could you help me? thanks

    ReplyDelete
  2. Hi,
    What the logCat looks like?
    Do you have set the permission in your manifest ?

    ReplyDelete
  3. &ltuses-permission android:name="android.permission.WAKE_LOCK" >
    &lt/uses-permission>

    ReplyDelete
  4. Thanks, yes it was because of the permission, now i have the problem that it never locks, the permission could go anywhere in the manifest? or something is wrong with my onResume/onPause? Thanks again

    ReplyDelete