Android中判斷網路連接是否可用及監控網路狀態

來源:互聯網
上載者:User

標籤:

來源:http://www.jb51.net/article/32920.htm

 

擷取網路資訊需要在AndroidManifest.xml檔案中加入相應的許可權。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

 1 public boolean isNetworkConnected(Context context) {  2 if (context != null) {  3  4 ConnectivityManager mConnectivityManager = (ConnectivityManager) context  5 .getSystemService(Context.CONNECTIVITY_SERVICE);  6  7 NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();  8  9 if (mNetworkInfo != null) { 10 11 return mNetworkInfo.isAvailable(); 12 13 } 14 15 } 16 17 return false; 18 }
一,判斷是否有網路連接
 1 public boolean isWifiConnected(Context context) {  2 if (context != null) {  3  4 ConnectivityManager mConnectivityManager = (ConnectivityManager) context  5 .getSystemService(Context.CONNECTIVITY_SERVICE);  6  7 NetworkInfo mWiFiNetworkInfo = mConnectivityManager  8 .getNetworkInfo(ConnectivityManager.TYPE_WIFI);  9 10 if (mWiFiNetworkInfo != null) { 11 12 return mWiFiNetworkInfo.isAvailable(); 13 14 } 15 16 } 17 18 return false; 19 }
二,判斷WIFI網路是否可用
 1 public boolean isMobileConnected(Context context) {  2  3 if (context != null) {  4  5 ConnectivityManager mConnectivityManager = (ConnectivityManager) context  6 .getSystemService(Context.CONNECTIVITY_SERVICE);  7  8 NetworkInfo mMobileNetworkInfo = mConnectivityManager  9 .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 10 11 if (mMobileNetworkInfo != null) { 12 13 return mMobileNetworkInfo.isAvailable(); 14 15 } 16 17 } 18 19 return false; 20 }
三,判斷移動網路是否可用
 1 public static int getConnectedType(Context context) {  2  3 if (context != null) {  4  5 ConnectivityManager mConnectivityManager = (ConnectivityManager) context  6 .getSystemService(Context.CONNECTIVITY_SERVICE);  7  8 NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();  9 10 if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { 11 12 return mNetworkInfo.getType(); 13 14 } 15 16 } 17 18 return -1; 19 }
四,擷取當前網路連接的類型資訊

在開發android應用時,涉及到要進行網路訪問,時常需要進行網路狀態的檢查,以提供給使用者必要的提醒。一般可以通過ConnectivityManager來完成該工作。
ConnectivityManager有四個主要任務
1、監聽行動電話通訊狀態(包括GPRS,WIFI, UMTS等)
2、手機狀態發生改變時,發送廣播
3、當一個網路連接失敗時進行故障切換
4、為應用程式提供可以擷取可用網路的高精度和粗糙的狀態
當我們要在程式中監聽網路狀態時,只要一下幾個步驟即可:
1、定義一個Receiver重載其中的onReceive函數,在其中完成所需要的功能,如根據WIFI和GPRS是否斷開來改變空間的外觀 

 1 connectionReceiver = new BroadcastReceiver() {  2 @Override  3 public void onReceive(Context context, Intent intent) {  4 ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);  5 NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  6 NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  7 if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {  8 Log.i(TAG, "unconnect");  9 // unconnect network 10 }else { 11 // connect network 12 } 13 } 14 };
View Code

2、在適當的地方註冊Receiver,可以在程式中註冊,在onCreate中調用如下函數即可:

1 IntentFilter intentFilter = new IntentFilter(); 2 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 3 registerReceiver(connectionReceiver, intentFilter);
View Code

3、在適當時取消註冊Receiver,可以在程式中取消,在onDestroye中調用如下函數即可:

1 if (connectionReceiver != null) { 2 unregisterReceiver(connectionReceiver); 3 }
View Code

 

Android中判斷網路連接是否可用及監控網路狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.