阻止我的应用进入休眠状态(Cordova/Android) [英] Stop my app from sleeping (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:
但没有运气.
推荐答案
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屋!