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

时间:2014-04-15 09:30:34

标签: android cordova

我正在使用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

2 个答案:

答案 0 :(得分:4)

确保正确安装网络电话插件

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();
    }
}

答案 1 :(得分:2)

退出和app:

navigator.app.exitApp();

确保将以下内容添加到config.xml中:

<gap:plugin name="org.apache.cordova.device"/> 
<feature name="http://api.phonegap.com/1.0/device"/>