Android網路程式設計(二)ConnectivityManager和NetworkInfo詳解
阿新 • • 發佈:2019-01-02
一. ConnectivityManager詳解
概要
ConnectivityManager是網路連線相關的管理器,它主要用於查詢網路狀態並在網路發生改變時發出狀態變化通知。這個類主要負責的下列四個方面:
1. 監控網路狀態(包括WiFi, GPRS, UMTS等)。
2. 當網路連線改變時傳送廣播Intent。
3. 當一種網路斷開時,試圖連線到另一種網路進行故障處理。
4. 提供一系列介面讓應用程式查詢可獲得的網路的粗粒度和細粒度狀態。
比較重要的幾個類常量
int | The Bluetooth data connection. 藍芽資料連線 |
int | The Ethernet data connection. 乙太網資料連線 |
int | The Mobile data connection. 移動資料鏈接 |
int | The WIFI data connection. wifi連結 |
比較重要的方法
boolean | Returns if the currently active data network is metered. |
static boolean | isNetworkTypeValid(int networkType) 判斷給定的數值是否表示一種網路 |
boolean |
requestRouteToHost |
void | Specifies the preferred network type. |
int | Tells the underlying networking system that the caller wants to begin using the named feature. |
int | Tells the underlying networking system that the caller is finished using the named feature. |
NetworkInfo是一個描述網路狀態的介面,可通過ConnectivityManager呼叫getActiveNetworkInfo()獲得當前連線的網路型別。
NetworkInfo有兩個列舉型別的成員變數NetworkInfo.DetailedState和NetworkInfo.State,用於檢視當前網路的狀態。其中NetworkInfo.State的值包括:
NetworkInfo.DetailedState則狀態描述更為詳細。NetworkInfo還包括一系列可用的方法用於判斷當前網路是否可用,如下:
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Report the extra information about the network state, if any was provided by the lower networking layers., if one is available. | |||||||||||
getReason() 如果資料網路連線可用,但是連線失敗,則通過此方法可獲得嘗試連結失敗的原因 Report the reason an attempt to establish connectivity failed, if one is available. | |||||||||||
getState() 獲取網路連線的粗粒度狀態 Reports the current coarse-grained state of the network. | |||||||||||
int | Return a network-type-specific integer describing the subtype of the network. | ||||||||||
Return a human-readable name describing the subtype of the network. | |||||||||||
int |
getType()報告當前網路從屬的網路型別
Reports the type of network to which the info in this NetworkInfo pertains.
|
||||||||||
getTypeName()報告當前網路從屬的網路型別,更明確的方式如wifi,和mobile等。 Return a human-readable name describe the type of the network, for example "WIFI" or "MOBILE". | |||||||||||
boolean | Indicates whether network connectivity is possible. | ||||||||||
boolean | isConnected() 判斷當前網路是否存在,並可用於資料傳輸 Indicates whether network connectivity exists and it is possible to establish connections and pass data. | ||||||||||
boolean | Indicates whether network connectivity exists or is in the process of being established. | ||||||||||
boolean | Indicates whether the current attempt to connect to the network resulted from the ConnectivityManager trying to fail over to this network following a disconnect from another network. | ||||||||||
boolean | isRoaming() 判斷裝置當前是否在網路上漫遊 Indicates whether the device is currently roaming on this network. | ||||||||||
toString() 返回一個包含該網路的簡單的易懂的字串描述。 Returns a string containing a concise, human-readable description of this object. |
一般來說很少需要用到所有的內容。最後奉上一點點乾貨:
public class NetWorkUtil {
private static String LOG_TAG = "<span style="font-family: Arial, Helvetica, sans-serif;">NetWorkUtil</span><span style="font-family: Arial, Helvetica, sans-serif;">";</span>
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
Log.w(LOG_TAG, "無法獲得ConnectivityManager");
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].isAvailable()) {
return true;
}
}
}
}
return false;
}
public static boolean checkNetState(Context context){
boolean netstate = false;
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)
{
netstate = true;
break;
}
}
}
}
return netstate;
}
public static boolean isNetworkRoaming(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
Log.w(LOG_TAG, "couldn't get connectivity manager");
} else {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null
&& info.getType() == ConnectivityManager.TYPE_MOBILE) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null && tm.isNetworkRoaming()) {
return true;
} else {
}
} else {
}
}
return false;
}
public static boolean isMobileDataEnable(Context context) throws Exception {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isMobileDataEnable = false;
isMobileDataEnable = connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
return isMobileDataEnable;
}
public static boolean isWifiDataEnable(Context context) throws Exception {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isWifiDataEnable = false;
isWifiDataEnable = connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
return isWifiDataEnable;
}
}