2014-04-15 43 views
2

我正在開發一個使用JQuery Mobile和Phonegap 3.4.0(Apache cordova)的android應用程序。我試圖關閉程序,所以我嘗試應用:如何以編程方式在Phonegap 3.4.0上退出應用程序?

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

+0

你收到了你的提醒信息嗎? – David

+0

是的,我收到警報消息,然後應用程序正常啓動 – Naili

+0

因此,您的onDeviceReady事件被解僱。試着在Phonegap社區論壇上看看這篇文章,它可以幫助:http://community.phonegap.com/nitobi/topics/how_to_exit_from_the_phonegap_app_on_android_and_ios – David

回答

2

確保安裝網絡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(); 
    } 
} 
1

要退出應用程序:

navigator.app.exitApp(); 

確保以下內容添加到您的配置。 xml:

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