簡體   English   中英

如果沒有使用 Phonegap 的 inte.net 連接,如何顯示警告框?

[英]how to display the alert box if there is no internet connection using Phonegap?

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

                }

在這里,如果只有 inte.net 連接不可用,我想顯示警報?我正在使用 if statment if(statusValue == 'none'){ alert }但這里不能在我的移動應用程序中工作。我在這里使用Phonegap- 1.3.0.js

做就是了

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

試試這個:

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

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

這應該可以幫助您:

// 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;
}

試試下面的代碼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM