Can I prevent phone from sleep on a webpage

2020-01-29 14:17发布

In app I can use http://developer.android.com/reference/android/os/PowerManager.WakeLock.html but is there a way to keep webpage running and prevent from going to sleep? It would be nice if it runs at least on android.

3条回答
我命由我不由天
2楼-- · 2020-01-29 14:58

In an app there are a couple of ways you can do it, but I guess you mean just in a mobile web page, viewed in any browser via Android. With normal HTML/Javascript/etc., I really, really doubt it.

It actually may be possible using Flash (on flash-enabled phones with plugins enabled), though, at least in specific circumstances. I say this because, in a test app without the WAKE_LOCK permission, loading this swf file into a WebView caused the following exception on some devices:

java.lang.SecurityException: Neither user ##### nor current process has android.permission.WAKE_LOCK

Even if this did work, however, it would run the risk of crashing apps or browsers that did not have the WAKE_LOCK permission. It may be possible due to bad code in the Adobe Flash Player plugin, rather than any intentional functionality.

查看更多
▲ chillily
3楼-- · 2020-01-29 15:07

You can use: https://github.com/richtr/NoSleep.js

Prevent display sleep and enable wake lock in any Android or iOS web browser.

Note that the library has some reliability/performance issues on some platforms/browsers. Users have found solutions that are listed in the issue comments and pull requests, but they have not been added since the repo owner appears not to be active currently.

It's recommended that you check those pull requests (and/or issues) for potential improvements before using in production.

查看更多
不美不萌又怎样
4楼-- · 2020-01-29 15:18

On iOS devices, just refreshing the page in Javascript every few seconds will keep the screen awake. This seems to be the correct strategy, hopefully Android will adopt this in a future version.

查看更多
登录 后发表回答