gpt4 book ai didi

android - 防止Cordova Android在后台运行

转载 作者:行者123 更新时间:2023-11-29 23:46:35 27 4
gpt4 key购买 nike

当 Cordova 应用移到后台时,是否有办法停止或暂停它?

带有简单的测试代码

var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},

// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
this.receivedEvent('deviceready');
},

// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');

console.log('Received Event: ' + id);
setInterval(function(){
console.log("Stilling running at",new Date());
},1);
}
};

app.initialize();

应用程序在手机上最小化后,间隔继续运行。有什么方法可以像在旧版 Android 中那样暂停应用程序吗?

这是一个使用 PouchDb 的大得多的应用程序的过度简化示例,但即使在应用程序最小化或屏幕关闭后,javascript 仍继续运行也存在同样的问题。

最佳答案

这取决于您所说的“暂停应用”的实际含义。应用程序由手机的操作系统管理,其行为因设备和操作系统版本而异。您可以做的是在应用程序被发送到后台和/或恢复到前台时运行代码。为此,您可以使用 cordova 生成的暂停和恢复事件。

您可以在此处找到 Activity 文档:Cordova Events

当应用程序发送到后台时运行代码

document.addEventListener("pause", onPause, false);

function onPause() {
// Stop any intervals here along with any other code to "pause" the app
}

当应用程序恢复到前台时运行代码

document.addEventListener("resume", onResume, false);

function onResume() {
// Start up any intervals you want along with any other code to restore the app
}

关于android - 防止Cordova Android在后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51286663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com