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_LOCK, WAVE_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.
Retrouvez moi sur Google+
Suivez moi sur Twitter
Rejoignez mon réseau LinkedIn ou Viadeo
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
ReplyDeleteHi,
ReplyDeleteWhat the logCat looks like?
Do you have set the permission in your manifest ?
This permission:
ReplyDelete<uses-permission android:name="android.permission.WAKE_LOCK" >
ReplyDelete</uses-permission>
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