prevent mobile from going into sleep mode when app is running

老子叫甜甜 提交于 2019-12-01 15:59:36

问题


I want to prevent Android Mobile from going into sleep mode when my threads are sending HTTP request. as what happened while threads are doing HTTP calls mobiles goes to sleep mode and when user wakes up the phone threads never complete.

User has to restart the app. what to do? please help


回答1:


You need a WakeLock. There are different kinds of wake locks so be sure to select the least aggressive one that meets your needs. In particular it sounds like you need a Partial Wake Lock.

Partial Wake Lock - Wake lock that ensures that the CPU is running. The screen might not be on.

Also, make sure you add the permission android.permission.WAKE_LOCK to your manifest. And finally be double sure to Release your lock when you are done.




回答2:


To prevent the phone from sleeping you can use a WakeLock but you should be careful when doing this to not kill the user's battery. If the phone goes to sleep does the user really care if your app finishes the requests it was making?

You will also need the WAKE_LOCK permission.




回答3:


for JAVA: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
for KOTLIN: window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Use this code, it will keep your device wake up and you didn't need any permission.



来源:https://stackoverflow.com/questions/5155720/prevent-mobile-from-going-into-sleep-mode-when-app-is-running

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