2017-04-27 97 views
0

我使用android設備如何檢查何時網絡在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,請在我的設備上連接無線LAN。

當我的設備上斷開無線局域網。我的來源是檢查wifi.isAvailable()

總之,

  1. 如何檢查時,網絡不可用(電流源,當網絡不可用檢查wifi.isAvailable)

  2. 當連接無線局域網和Wifi關閉,請檢查wifi.isAvailable()但是當斷開無線局域網和Wifi的連接時,請檢查wifi.isAvailable() 如何區分這種情況。

謝謝。

回答

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

你應該使用getActiveNetworkInfo()來檢查當前活動的默認數據網絡的連通性 - 無需檢查特定的數據網絡;無論是以太網還是Wi-Fi。

public static boolean NetAvailable(final Context context) { 
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
    final boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); 
    return isConnected; 
    } 
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); 
} 
0
public static boolean isNetworkAvailable(Context context) { 
     ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
       Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
     return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
    } 
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; 

}

如果此代碼返回的失敗結果意味着網絡不可用,並且調用您的代碼以獲取可用網絡。

+0

我認爲這段代碼將返回「主線程異常的android os網絡」 –

+0

@MohammadHaidar你是對的,從工作線程調用這個 – Shrikant

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()和在的onDestroy摧毀它()

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

} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    unregisterReceiver(internetConnectionReciever); 
} 
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()

+0

' wifi!= null'是否可以wifi列表檢查? – chohyunwook

+0

我不明白你的問題,但你想要可用的無線列表? –

+0

檢查http://stackoverflow.com/questions/18741034/how-to-get-available-wifi-networks-and-display-them-in-a-list-in-android&http://stackoverflow.com/問題/ 11898409/list-of-available-wifi-devices獲取免費wifi列表 –

相關問題