Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I prevent phone from sleep on a webpage

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.

like image 261
set Avatar asked May 24 '11 06:05

set


4 Answers

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.

like image 198
Venryx Avatar answered Nov 01 '22 14:11

Venryx


You can use the Wake Lock web API (check support)

https://web.dev/wakelock/

like image 27
Luciano Júnior Avatar answered Nov 01 '22 14:11

Luciano Júnior


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.

like image 40
Sven Viking Avatar answered Nov 01 '22 14:11

Sven Viking


Play fake looped VIDEO or AUDIO on your page

You can use this a quick example to add a looped video with fake data to your page and prevent mobile device from sleep:

// Create the root video element
var video = document.createElement('video');
video.setAttribute('loop', '');
// Add some styles if needed
video.setAttribute('style', 'position: fixed;');

// A helper to add sources to video
function addSourceToVideo(element, type, dataURI) {
    var source = document.createElement('source');
    source.src = dataURI;
    source.type = 'video/' + type;
    element.appendChild(source);
}

// A helper to concat base64
var base64 = function(mimeType, base64) {
    return 'data:' + mimeType + ';base64,' + base64;
};

// Add Fake sourced
addSourceToVideo(video,'webm', base64('video/webm', 'GkXfo0AgQoaBAUL3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AAGyTGF2YzUyLjg3LjQGAQIAAAAYc3R0cwAAAAAAAAABAAAAAQAAAAAAAAAcc3RzYwAAAAAAAAABAAAAAQAAAAEAAAABAAAAFHN0c3oAAAAAAAAAEwAAAAEAAAAUc3RjbwAAAAAAAAABAAAALAAAAGB1ZHRhAAAAWG1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAG1kaXJhcHBsAAAAAAAAAAAAAAAAK2lsc3QAAAAjqXRvbwAAABtkYXRhAAAAAQAAAABMYXZmNTIuNzguMw=='));

// Append the video to where ever you need
document.body.appendChild(video);

// Start playing video after any user interaction.
// NOTE: Running video.play() handler without a user action may be blocked by browser.
var playFn = function() {
    video.play();
    document.body.removeEventListener('touchend', playFn);
};
document.body.addEventListener('touchend', playFn);
like image 6
Andrii Verbytskyi Avatar answered Nov 01 '22 13:11

Andrii Verbytskyi