7

I need to do on the website some feature to disabled idle/sleep phone. Does anyone try make this on phone with android ? is it in any way possible?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
AdamM
  • 163
  • 1
  • 1
  • 9
  • Why do you want to prevent sleep on a website? – Louis CAD Nov 26 '15 at 08:25
  • 1
    @LouisCAD Certain web apps are designed to be active while the user is performing an activity, like cooking or exercising. – Šime Vidas Dec 02 '15 at 07:06
  • @LouisCAD when uploading a file it's good to keep the page active. – ile Jan 19 '17 at 08:21
  • Possible duplicate of [Can I prevent phone from sleep on a webpage](https://stackoverflow.com/questions/6106747/can-i-prevent-phone-from-sleep-on-a-webpage) – Venryx Jul 29 '19 at 03:31

3 Answers3

8

We strongly don't encourage developers to do this at all. However it is possible. You can simply have a video playing on the page and the device won't go to sleep. This means you could have single frame video set to auto-loop and play (requires a user interaction)

Richard Tibbett has created NoSleep.js to simplify the process for developers.

Kinlan
  • 16,315
  • 5
  • 56
  • 88
  • 6
    There are valid use cases. You cannot just not provide an API and then discourage devs from using hacks :-P – Šime Vidas Dec 02 '15 at 07:10
  • It will be intranet application so no one hurts ;) Thanks – AdamM Dec 09 '15 at 09:02
  • Use case: **automated tests** take minutes to run, and sleep suspends the tests. So I put a "video" on the test page, and pause it when the tests are done. – Bob Stein Mar 29 '18 at 22:51
7

JavaScript in Chrome on Android (7.0) indeed shuts down after 5 min in sleep mode. Aaargh!

To prevent that, we need e.g. an audio object:

<audio id="dummyAudio">
   <source src="silent.ogg" type="audio/ogg">
   <source src="silent.mp3" type="audio/mpeg">
</audio>

and play it at regular intervals:

function playDummyAudio() { dummyAudio.play(); }    
$(function() {
   var dummyAudio = document.querySelector('#dummyAudio');
   window.setInterval(playDummyAudio, 60 * 1000);
}

Note that the Audio object has to be "unlocked" in a user gesture callback. This can be accomplished e.g. by having a grey CSS overlay with a big fat dummy "Start" button, whose onClick() callback only hides the overlay and calls dummyAudio.load().

teg_brightly
  • 468
  • 6
  • 20
Per Lindberg
  • 737
  • 8
  • 8
1

There is an experimental implementation of Wake Lock API (http://www.w3.org/TR/wake-lock/) in Chromium starting I believe from version 48.0.2551.0. Though this only works when the experimental features are enabled in the browser e.g. via --enable-experimental-web-platform-features command line switch, so this is not yet useful for the general audience. In the meantime I think it is possible to use the video playback trick as suggested by Kinlan.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Andrey Logvinov
  • 119
  • 1
  • 6