1.判断网络是否可用
private boolean isNetworkAvaiable() {
ConnectivityManager connectivityManager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (null != networkInfo && networkInfo.isConnected()) {
return true;
}
return false;
}
2.获取当前网络的类型
private String currentNetworkType() {
ConnectivityManager connectivityManager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivityManager) {
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
if (null != networkInfo) {
return networkInfo.getTypeName();
}
}
return null;
}
3.判断当前网络类型是否是WIFI(3G)
private boolean isNetworkTypeWIFI() {
ConnectivityManager connectivityManager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivityManager) {
NetworkInfo networkInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);//判断网络类型是否是3G时,用TYPE_MOBILE即可
if (null != networkInfo && networkInfo.isConnected()) {
return true;
}
}
return false;
}
4.跳转至网络设置界面
如果网络不可用时,我们需要需要提示用户是否跳转到网络设置界面以开启网络。这时我们需要用到下面这两个Intent:
new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS) // 跳转到无线网络设置界面
new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS) // 跳转到无线wifi网络设置界面
5.备注
我们查看网络状态的时候,需要在mainifest文件中加上权限:
<!-- 获取网络状态权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
6.附NetworkInfo的方法摘要
NetworkInfo.DetailedState |
getDetailedState()
Reports the current fine-grained state of the network.
|
String |
getExtraInfo()
Report the extra information about the network state, if any was provided by the lower networking layers., if one is available.
|
String |
getReason()
Report the reason an attempt to establish connectivity failed, if one is available.
|
NetworkInfo.State |
getState()
Reports the current coarse-grained state of the network.
|
int |
getSubtype()
Return a network-type-specific integer describing the subtype of the network.
|
String |
getSubtypeName()
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.
|
String |
getTypeName()
Return a human-readable name describe the type of the network, for example "WIFI" or "MOBILE".
|
boolean |
isAvailable()
Indicates whether network connectivity is possible.
|
boolean |
isConnected()
Indicates whether network connectivity exists and it is possible to establish connections and pass data.
|
boolean |
isConnectedOrConnecting()
Indicates whether network connectivity exists or is in the process of being established.
|
boolean |
isFailover()
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.
|
String |
toString()
Returns a string containing a concise, human-readable description of this object.
|