我希望能够在获得用户许可的情况下始终保持我的cordova 应用程序处于活动状态。更多的“黑客”解决方案也很好,例如每 X 分钟重新激活应用程序。我尝试了很多插件,但都没有成功。下面是我的代码。我有小米 readmi note 8 pro 和应用程序,我已经设置了应用程序,以便对电池使用没有限制。 我可以让它最多持续一个小时。 我应该在我的代码中更改什么?或者我可以在android上更改一些设置吗? 谢谢。
<platform name="android">
<allow-intent href="market:*" />
<config-file parent="./" target="app/src/main/AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</config-file>
</platform>
js
document.addEventListener('deviceready', onDeviceReady, false);
function callajax(text){
$.ajax({
url: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
method: "POST",
data: {txt: text},
success: function(e){
console.log("success ajax",JSON.stringify(e));
},
error: function(e){
console.log("error ajax",JSON.stringify(e));
}
});
}
async function onDeviceReady() {
/***************************************************************/
//https://github.com/transistorsoft/react-native-background-fetch
var BackgroundFetch = window.BackgroundFetch;
// Your BackgroundFetch event handler.
var onEvent = async function(taskId) {
console.log('[BackgroundFetch] event received: ', taskId);
callajax("BackgroundFetch onEvent");
// Required: Signal completion of your task to native code
// If you fail to do this, the OS can terminate your app
// or assign battery-blame for consuming too much background-time
BackgroundFetch.finish(taskId);
};
// Timeout callback is executed when your Task has exceeded its allowed running-time.
// You must stop what you're doing immediately BackgroundFetch.finish(taskId)
var onTimeout = async function(taskId) {
callajax("onEvent TIMEOUT");
console.log('[BackgroundFetch] TIMEOUT: ', taskId);
BackgroundFetch.finish(taskId);
};
var status = await BackgroundFetch.configure({minimumFetchInterval: 15}, onEvent, onTimeout);
console.log('[BackgroundFetch] configure status: ', status);
/***************************************************************/
//https://github.com/katzer/cordova-plugin-background-mode
//cordova.plugins.backgroundMode.setDefaults({ silent: true });
cordova.plugins.backgroundMode.enable();
cordova.plugins.backgroundMode.on('activate', function() {
cordova.plugins.backgroundMode.disableWebViewOptimizations();
});
/***************************************************************/
//https://github.com/boltex/cordova-plugin-powermanagement
window.powerManagement.dim(function() {
console.log('Wakelock acquired');
}, function() {
console.log('Failed to acquire wakelock');
});
window.powerManagement.setReleaseOnPause(false, function() {
console.log('setReleaseOnPause successfully');
}, function() {
console.log('Failed to set');
});
/***************************************************************/
setInterval(function(){
callajax("test");
},60000 * 5);
}