Naili
Naili

Reputation: 1602

How to exit an application programmatically on Phonegap 3.4.0?

I'm developing an android application using JQuery Mobile and Phonegap 3.4.0 (Apache cordova). I trying to close the application programmatically so I tried :

navigator.app.exitApp();

and

navigator.device.exitApp();

This is the ondeviceready document listener :

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

I'm running the application directly on my android phone, and when I try to check the value of navigator.app I'm getting undefined

Upvotes: 3

Views: 5597

Answers (2)

renanleandrof
renanleandrof

Reputation: 6997

To exit and app:

navigator.app.exitApp();

Make sure to add the following to your config.xml:

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

Upvotes: 3

Nurdin
Nurdin

Reputation: 23883

Make sure you install network phonegap plugin properly

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

Then use this code

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

Upvotes: 4

Related Questions