如何在拨打电话前检查网络可用性?

时间:2012-07-21 12:23:31

标签: android call offline

我想拨打电话,但在此操作之前,我必须确保移动网络可用并且手机未处于飞行模式。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您需要在清单中使用ACCESS_NETWORK_STATE权限,然后使用此权限:

public boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    // if no network is available networkInfo will be null, otherwise check if we are connected
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}       

对于所有类型的连接,您可以尝试使用此(java script):

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

checkConnection();     
  

连接对象可以访问设备的蜂窝和wifi   连接信息。

参考文献:
vogelle-de
how to programmatically check availability of internet connection using jquery