1. 程式人生 > >判斷Wifi狀態是否可用

判斷Wifi狀態是否可用

獲取網路資訊需要在AndroidManifest.xml檔案中加入相應的許可權。 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />1)判斷是否有網路連線

複製程式碼

 1 public boolean isNetworkConnected(Context context) { 
 2 if (context != null) { 
 3 ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
 4 .getSystemService(Context.CONNECTIVITY_SERVICE); 
 5 NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
 6 if (mNetworkInfo != null) { 
 7 return mNetworkInfo.isAvailable(); 
 8 } 
 9 } 
10 return false; 
11 }

2)判斷WIFI網路是否可用

複製程式碼

 1 public boolean isWifiConnected(Context context) { 
 2 if (context != null) { 
 3 ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
 4 .getSystemService(Context.CONNECTIVITY_SERVICE); 
 5 NetworkInfo mWiFiNetworkInfo = mConnectivityManager 
 6 .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
 7 if (mWiFiNetworkInfo != null) { 
 8 return mWiFiNetworkInfo.isAvailable(); 
 9 } 
10 } 
11 return false; 
12 }

3)判斷MOBILE網路是否可用

複製程式碼

 1 public boolean isMobileConnected(Context context) { 
 2 if (context != null) { 
 3 ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
 4 .getSystemService(Context.CONNECTIVITY_SERVICE); 
 5 NetworkInfo mMobileNetworkInfo = mConnectivityManager 
 6 .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
 7 if (mMobileNetworkInfo != null) { 
 8 return mMobileNetworkInfo.isAvailable(); 
 9 } 
10 } 
11 return false; 
12 }

4)獲取當前網路連線的型別資訊

複製程式碼

 1 public static int getConnectedType(Context context) { 
 2 if (context != null) { 
 3 ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
 4 .getSystemService(Context.CONNECTIVITY_SERVICE); 
 5 NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
 6 if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { 
 7 return mNetworkInfo.getType(); 
 8 } 
 9 } 
10 return -1; 
11 }

有時候我們連線上一個沒有外網連線的WiFi或者有線就會出現這種極端的情況,目前Android SDK還不能識別這種情況,一般的解決辦法就是ping一個外網。

複製程式碼

 1 /* @author suncat
 2  * @category 判斷是否有外網連線(普通方法不能判斷外網的網路是否連線,比如連線上區域網)
 3  * @return
 4  */ 
 5 public static final boolean ping() { 
 6 
 7   String result = null; 
 8   try { 
 9       String ip = "www.baidu.com";// ping 的地址,可以換成任何一種可靠的外網 
10       Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping網址3次 
11       // 讀取ping的內容,可以不加 
12       InputStream input = p.getInputStream(); 
13       BufferedReader in = new BufferedReader(new InputStreamReader(input)); 
14       StringBuffer stringBuffer = new StringBuffer(); 
15       String content = ""; 
16       while ((content = in.readLine()) != null) { 
17           stringBuffer.append(content); 
18       } 
19       Log.d("------ping-----", "result content : " + stringBuffer.toString()); 
20       // ping的狀態 
21       int status = p.waitFor(); 
22       if (status == 0) { 
23           result = "success"; 
24           return true; 
25       } else { 
26           result = "failed"; 
27       } 
28   } catch (IOException e) { 
29       result = "IOException"; 
30   } catch (InterruptedException e) { 
31       result = "InterruptedException"; 
32   } finally { 
33       Log.d("----result---", "result = " + result); 
34   } 
35   return false;
36 }