How do I prevent an Android device from going to sleep programmatically?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
One option is to use a wake lock. Example from the docs:
There’s also a table on this page that describes the different kinds of wakelocks.
Be aware that some caution needs to be taken when using wake locks. Ensure that you always
release()the lock when you’re done with it (or not in the foreground). Otherwise your app can potentially cause some serious battery drain and CPU usage.The documentation also contains a useful page that describes different approaches to keeping a device awake, and when you might choose to use one. If “prevent device from going to sleep” only refers to the screen (and not keeping the CPU active) then a wake lock is probably more than you need.
You also need to be sure you have the WAKE_LOCK permission set in your manifest in order to use this method.