如何检查Android设备上何时没有网络?

时间:2017-04-27 06:26:30

标签: android networking

我使用的是Android设备

我希望在应用启动时检查当前的网络状态。

所以,我试试这个。

ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERIVCE);
NetworkInfo ethernet = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (wifi.isConnected()) {
   Log.d(TAG, "WIFI isConnected");
} else if (ethernet.isConnected()) {
   Log.d(TAG, "Ethernet isConnected");
} else if (wifi.isAvaiable()) {
   Log.d(TAG, "Wifi not connect, but wifi isAvailable");
} else {
   Log.d(TAG, "not available network");
}

此源检查Wi-Fi和以太网网络状态。

但无法联网时。不检查。

我的设备如果使用wifi,请在我的设备上连接无线局域网。

断开设备上的无线局域网。我的来源是检查wifi.isAvailable()

总之,

  1. 如何检查网络何时不可用(当前来源,当网络不可用时检查wifi.isAvailable)

  2. 关闭无线局域网和Wifi时,请检查wifi.isAvailable()但是当关闭无线局域网和Wifi时,请检查wifi.isAvailable() 如何区分这种情况。

  3. 感谢。

7 个答案:

答案 0 :(得分:0)

您应该使用getActiveNetworkInfo()来检查当前活动的默认数据网络的连接性 - 无需检查特定的数据网络;无论是以太网还是Wi-Fi。

$this->data->subject

答案 1 :(得分:0)

尝试此功能

public boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

并像这样检查

if(isNetworkAvailable(this))
{
System.out.println(""Internet Connected);
}else
{
System.out.println(""Internet Not Connected);
}

答案 2 :(得分:0)

public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

答案 3 :(得分:0)

了解正常工作的互联网状态使用此代码段

public static boolean isInternetAccess() {
    try {
        HttpURLConnection urlc = (HttpURLConnection) 
            (new URL("http://www.google.com")
            .openConnection());
        urlc.setRequestProperty("User-Agent", "Android");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1500); 
        urlc.connect();
        return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
        Log.e(TAG, "Error checking internet connection", e);
    }

return false;

}

如果此代码返回失败结果,则表示网络不可用,并调用您的代码以获取可用网络。

答案 4 :(得分:0)

您需要使用BroadcastReceiver进行检查连接。当状态改变时,它调用onReceive()方法。

public BroadcastReceiver internetConnectionReciever = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        @SuppressWarnings("static-access")
        NetworkInfo activeWIFIInfo = connectivityManager
                .getNetworkInfo(connectivityManager.TYPE_WIFI);

        if (activeWIFIInfo.isConnected() || activeNetInfo.isConnected()) {

        } 
    }
};

在onResume()中注册Receiver并在onDestroy()

中销毁它
 @Override
protected void onResume() {
    super.onResume();
    registerReceiver(internetConnectionReciever, new IntentFilter(
            "android.net.conn.CONNECTIVITY_CHANGE"));

}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(internetConnectionReciever);
}

答案 5 :(得分:0)

   public static boolean checkInternetConnection(Context context) {
        try {
            ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            android.net.NetworkInfo ethernet = connec.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);

            if ((wifi != null && wifi.isConnected())
                    || (mobile != null && mobile.isConnected())
                    || (ethernet != null && ethernet.isConnected())) {
                return true;
            }
            log("Connection", "Connection failed");
            return false;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return false;
        }
    }   

使用此方法。你不需要检查wifi.isAvailable(),只需检查isConnected()

答案 6 :(得分:-1)

尝试此操作以在线或离线检查设备。

private boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting()){
        return true;
    }else return false;
}