I'm trying to develop a very simple app that displays like a slideshow of images which Im sure ill be able to figure out (if anyone has any advice would be much appreciated) but what im wondering is there any way I can stop the device from going to sleep if its left cycling through these images?
Asked
Active
Viewed 2,517 times
1
-
1possible duplicate of [I want to prevent android phone from going to sleep using my code?](http://stackoverflow.com/questions/3723634/i-want-to-prevent-android-phone-from-going-to-sleep-using-my-code) – Fosco May 20 '11 at 16:49
3 Answers
6
using
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
will make the screen always on which will disable the lock. Remove it by using
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Hazem Farahat
- 3,790
- 2
- 26
- 42
-
If this needs to be set/cleared dynamically (e.g., based on a preference or menu choice), this is a good answer. The `android:keepScreenOn` attribute is a bit simpler if it always needs to be set. – CommonsWare May 20 '11 at 17:02
-
Yes you are right,, I just wanted to add a programmatic way beside your XML solution – Hazem Farahat May 20 '11 at 17:20
4
Add android:keepScreenOn="true"
to some widget in your activity's layout XML file. While the activity is in the foreground, the device will not fall asleep.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
2
You would use a wake lock.
http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

Ryan Olds
- 4,847
- 28
- 24
-
This is not the best option for things involving a UI. A `WakeLock` is for background processing. – CommonsWare May 20 '11 at 16:55
-