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]);

                }

ここで、インターネット接続のみが利用できない場合にアラートを表示したいですか?私は if ステートメントを使用してif(statusValue == 'none'){ alert }いますが、ここではモバイルアプリで動作していません。ここではPhonegap-1.3.0.jsを使用しています

4

5 に答える 5

4

ただする

networkState = navigator.network.connection.type; 
if (networkState == Connection.NONE)
{
  alert('No internet connection ');
};
于 2012-04-10T05:20:23.267 に答える
2

これを試して:

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]);
}
于 2013-10-04T05:39:03.163 に答える
1

また、navigator.network.isReachable を使用して、ほぼ 100% のアップタイムで google などの一部のサイトに ping を実行することもできます。

于 2012-04-10T04:57:05.753 に答える
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"); 
}
于 2014-07-16T06:49:39.940 に答える
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;
}
于 2013-08-01T19:33:48.633 に答える