Android: Change delay before the phone is put to sleep while my app is running

做~自己de王妃 提交于 2019-12-12 01:16:52

问题


This question sounds so easy I can't believe I can't find information on it (maybe I have the wrong key words in mind...)

I'm looking for a way to change the delay before the phone is put to sleep when my app is running.

I was using wake locks until now to prevent my app from being closed to frequently (its and opengl app and the loading time is a killer specially on slow phones). I don't like the idea of indefinitely leaving the phone on (mainly because it empties the battery fast to have a full opengl app running).

Is there a way then to change the delay before the phones goes to sleep to 2~3min ?

I would also like to add that I do not wish to change the settings of the phone (that is only the users decision)

Jason

edited to correct the term "application going to sleep" which was pointed out as being incorrect.


回答1:


Create a Timer object and release() your wake lock in the timer task. I'm relatively sure you can release a lock in a worker thread, but worst case, you'd need to use runOnUiThread if I'm incorrect.

Be sure to handle the case when onPause() is called and cancel your timer in that case and release the lock immediately.

In general, I'm not a fan of wake locks, but if this is to make your boss happy, wake-lock away.

EDIT: Btw, the system setting for the UI timeout is accessible by apps: SCREEN_OFF_TIMEOUT in android.provider.Settings.System. But really, this is for private applications only, public applications have no business changing this value.



来源:https://stackoverflow.com/questions/4676075/android-change-delay-before-the-phone-is-put-to-sleep-while-my-app-is-running

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!