阻止我的应用进入休眠状态(Cordova/Android) [英] Stop my app from sleeping (Cordova/Android)

查看:804
本文介绍了阻止我的应用进入休眠状态(Cordova/Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用,它是通过node.js webpack项目创建的.

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

当我在手机上安装我的应用程序时,我注意到它在手机进入睡眠状态时也进入了睡眠状态.因此,例如,我有一个javascript计时器,该计时器会停止调用:

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);

用于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.

更新

我按照此处所述的说明进行操作:

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

但没有运气.

推荐答案

供您使用的最佳选择是

The best option for you to use is the WakeLock api

manifest file中为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
}

并使用lock.unlock();功能释放锁.

OR

对于cordova应用程序,您还可以使用插件失眠要进行的更改config文件中的内容如文档中所述,可以按以下方式简单地用作

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

这篇关于阻止我的应用进入休眠状态(Cordova/Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆