2012-04-10 69 views
1
function checkConnection() { 
          var networkState = navigator.network.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.NONE]  = 'No network connection'; 


        alert('Connection type: ' + states[networkState]); 

       } 

這裏我要顯示警報如果只有互聯網連接是不是可用的編緝?我使用如果statment if(statusValue == 'none'){ alert }但這裏不是我的移動app.Here工作我使用的PhoneGap-1.3.0.js如果使用Phonegap沒有互聯網連接,如何顯示警報框?

回答

1

你也可以使用navigator.network.isReachable來ping一些網站,例如谷歌幾乎100%的正常運行時間。在所有的移動操作系統或某些特定版本的Android中,它是不是有用的?

+1

我使用的Android 4.0版本... navigator.network.isReachable現在更高version.i工作試過 – Raj 2012-04-10 04:58:17

4

只是做

networkState = navigator.network.connection.type; 
if (networkState == Connection.NONE) 
{ 
    alert('No internet connection '); 
}; 
+1

它不工作亞...我沒有得到警告框,如果我關掉互聯網 – Raj 2012-04-10 05:25:56

+3

如果您想在設備脫機時收到通知,那麼您需要註冊「脫機」事件。 http://docs.phonegap.com/en/1.3.0/phonegap_events_events.md.html#offline – 2012-04-10 15:49:28

1

這會幫助你:

// Wait for Cordova to load 
document.addEventListener("deviceready", onDeviceReady, false); 
// Cordova is ready 
function onDeviceReady() { 
document.addEventListener("offline", whenOffline, false); 
return false; 
} 

function whenOffline() { 
navigator.notification.alert(
'Sorry your internet connection is not working, please enable it !', // message 
alertDismissed, // callback 
'Settings', // title 
'Done' // buttonName 
); 
return false; 
} 
1

試試這個:

function checkConnection() { 
    network = navigator.network.connection.type; 


    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.NONE] = 'No network connection'; 
    // alert('Connection type: ' + states[network]); 
    return states[network]; 
} 
    if (states[network] == 'No network connection') 
{ 
    alert('Connection type: ' + states[network]); 
} 
1

嘗試下面的代碼。

var networkState = navigator.network.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.NONE]  = 'No network connection'; 

if ((states[networkState]) == states[Connection.NONE]) 
{ 
alert("Please check your internet connectivity and try again"); 
} 
相關問題