2010-09-26 110 views

回答

4
if (Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
     ConnectivityManager.TYPE_MOBILE) 
    || Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
     ConnectivityManager.TYPE_WIFI) { 

    // do something 
} 
+0

日Thnx的代碼 – 2010-09-27 15:43:05

+0

1,但獲得來自連接性服務「活動」網絡。 – mschonaker 2010-10-05 06:27:00

1

對於檢查網絡這個功能會幫助你。

`public boolean InternetConnection() 

{ 
     ConnectivityManager connectivity = (ConnectivityManager) this.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; 
    }` 

如果網絡存在,它將返回true,否則它將返回false。

0

可以檢查網絡的可用性或不

ConnectivityManager connectivityManager 
    = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();