Android 監(jiān)聽網(wǎng)絡狀態(tài)方法詳解
Android 監(jiān)聽網(wǎng)絡狀態(tài)方法詳解
一.加入網(wǎng)絡權(quán)限
獲取網(wǎng)絡信息需要在AndroidManifest.xml文件中加入相應的權(quán)限。
<uses-permission Android:name="android.permission.ACCESS_NETWORK_STATE" />
二.判斷手機網(wǎng)絡的幾個方案
1)判斷是否有網(wǎng)絡連接
public boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable();
}
}
return false;
}
2)判斷WIFI網(wǎng)絡是否可用
public boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.isAvailable();
}
}
return false;
}
4)獲取當前網(wǎng)絡連接的類型信息
public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}
在開發(fā)android應用時,涉及到要進行網(wǎng)絡訪問,時常需要進行網(wǎng)絡狀態(tài)的檢查,以提供給用戶必要的提醒。一般可以通過ConnectivityManager來完成該工作。
ConnectivityManager有四個主要任務:
1、監(jiān)聽手機網(wǎng)絡狀態(tài)(包括GPRS,WIFI, UMTS等)
2、手機狀態(tài)發(fā)生改變時,發(fā)送廣播
3、當一個網(wǎng)絡連接失敗時進行故障切換
4、為應用程序提供可以獲取可用網(wǎng)絡的高精度和粗糙的狀態(tài)
當我們要在程序中監(jiān)聽網(wǎng)絡狀態(tài)時,只要一下幾個步驟即可:
1、定義一個Receiver重載其中的onReceive函數(shù),在其中完成所需要的功能,如根據(jù)WIFI和GPRS是否斷開來改變空間的外觀
connectionReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
Log.i(TAG, "unconnect");
// unconnect network 這時提示用戶網(wǎng)絡斷開信息
}else {
// connect network 可以做一些網(wǎng)絡請求,刷新界面
}
}
};
2、在適當?shù)牡胤阶訰eceiver,可以在程序中注冊,在onCreate中調(diào)用如下函數(shù)即可
IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(connectionReceiver, intentFilter);
3、在適當時取消注冊Receiver,可以在程序中取消,在onDestroye中調(diào)用如下函數(shù)即可:
if (connectionReceiver != null) {
unregisterReceiver(connectionReceiver);
}
說了這么多。其實主要是通過ConnectivityManager獲取當前的網(wǎng)絡狀態(tài),或者監(jiān)聽網(wǎng)絡狀態(tài)的改變。這樣,當應用需要聯(lián)網(wǎng)時,網(wǎng)絡狀態(tài)發(fā)生改變可以及時提示用戶,或者當網(wǎng)絡重新連接時自動獲取網(wǎng)絡數(shù)據(jù)進行刷新。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android RefreshLayout實現(xiàn)下拉刷新布局
這篇文章主要為大家詳細介紹了Android RefreshLayout實現(xiàn)下拉刷新布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

