你用最好的選擇是WakeLock API
在manifest file
添加許可爲wakeLock
<uses-permission android:name="android.permission.WAKE_LOCK" />
,那麼你可以在以下根據您的修改添加類似的建議在MDN行爲
function forPingTimer(){
var lock = window.navigator.requestWakeLock('screen');
//set timeout or until the timer expires
}
並使用lock.unlock();
函數釋放鎖。
OR
對於科爾多瓦應用程序,你也可以使用插件insomnia在config
文件中所做的更改是在該文檔中提到,它可以通過以下方式來簡單地用作
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
您可能遇到的問題是打盹模式或節省電池。您需要讓最終用戶編輯應用設置,並且不允許打瞌睡。不,你不能以編程方式做到這一點。 – Eric