gpt4 book ai didi

android - 如何在 Phonegap 3.4.0 上以编程方式退出应用程序?

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:52 25 4
gpt4 key购买 nike

我正在使用 JQuery Mobile 和 Phonegap 3.4.0 (Apache cordova) 开发一个 android 应用程序。我试图以编程方式关闭应用程序,所以我尝试了:

navigator.app.exitApp();

navigator.device.exitApp();

这是 ondeviceready 文档监听器:

document.addEventListener("deviceready", onDeviceReady(), true);
function onDeviceReady() {
var today = new Date();
var dd = today.getDate();
if(dd==15) {
alert("Your application has been expired");
navigator.app.exitApp();
}
}

我直接在我的 android 手机上运行该应用程序,当我尝试检查 navigator.app 的值时,我得到了 undefined

最佳答案

确保正确安装网络phonegap插件

cordova plugin add https://github.com/apache/cordova-plugin-network-information

然后使用这段代码

document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//

function onDeviceReady() {
checkConnection();
}

function checkConnection() {
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
if ((states[networkState]) == states[Connection.NONE]) {
alert('No Internet Connection. Click OK to exit app');
navigator.app.exitApp();
}
}

关于android - 如何在 Phonegap 3.4.0 上以编程方式退出应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23079724/

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