最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stop my app from sleeping (CordovaAndroid) - Stack Overflow

programmeradmin1浏览0评论

I have an Android App which I create from a node.js webpack project.

When I install my app on my phone I notice that it sleeps when the phone sleeps. So for example, I have a javascript timer which stops getting called:

pingTimer=setInterval(ping,pingInterval);

for the pingInterval. How do I stop my app from sleeping? Eventually, I will want my app to go to sleep but for now stopping the phone from sleeping is my best option.

UPDATE

I followed the instructions as described here:

.html

but with no luck.

I have an Android App which I create from a node.js webpack project.

When I install my app on my phone I notice that it sleeps when the phone sleeps. So for example, I have a javascript timer which stops getting called:

pingTimer=setInterval(ping,pingInterval);

for the pingInterval. How do I stop my app from sleeping? Eventually, I will want my app to go to sleep but for now stopping the phone from sleeping is my best option.

UPDATE

I followed the instructions as described here:

http://www.greenbot.com/article/2993199/android/how-to-turn-off-doze-mode-for-specific-apps-in-android-marshmallow.html

but with no luck.

Share Improve this question edited Dec 16, 2016 at 14:33 Baz asked Dec 12, 2016 at 19:06 BazBaz 13.1k40 gold badges152 silver badges279 bronze badges 1
  • 1 The issue that you might have is the doze mode or battery saving. You need to have your end user edit the app setting and does not allow doze. No you can't do that programmatically. – Eric Commented Dec 12, 2016 at 19:18
Add a comment  | 

3 Answers 3

Reset to default 10 +100

The best option for you to use is the WakeLock api

Add the permission in the manifest file for the wakeLock

<uses-permission android:name="android.permission.WAKE_LOCK" />

then you can add the following according to your modifications like as suggested in the MDN behaviour

function forPingTimer(){
 var lock =  window.navigator.requestWakeLock('screen');
 //set timeout or until the timer expires
}

and release the lock using the lock.unlock(); function.

OR

For the cordova app you can also use the plugin insomniaThe changes to be made in the config file are as mentioned in the docs and it can be simply used in the following way as

function forPingTimer(){
//as long as the app runs or set the timeout here or wrap it in a promise
//Simply calling window.plugins.insomnia.keepAwake() to keep awake
}
//window.plugins.insomnia.allowSleepAgain() to sleep again until the timer after the timer is fulfilled

The easiest option to prevent your cordova android app from sleeping is to use the Insomnia-PhoneGap-Plugin. This plugin is also supported in most of the platforms like Android, iOS and windows.

This plugin is easy to use. keepAwake function in the plugin prevents the device from sleeping and allowSleepAgain function call allows the device to sleep again. You can find more info on the official insomnia plugin link.

Hope it helps. Cheers.

Just to be clear on the syntax, install the cordova-plugin-insomnia plugin. Then include a simple interval timer in your code:

// start an interval timer
var mainloopid = setInterval(mainloop, 1000);
function mainloop {
    // call the plugin every (say) one second to keep your app awake
    window.plugins.insomnia.keepAwake();
}

You must call the plugin's keepAwake method periodically (at least before your device want's to sleep.) This is a must for all display clocks, and the interval timer can be part of your timer that is used to update seconds in a clock. This solution is overriding the setting on your device that says, for example, "sleep after 15 seconds". It is the equivalent of having an Android Device setting that is "sleep never" (while your app is running).

发布评论

评论列表(0)

  1. 暂无评论