【Android】判断网络是否可用

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.





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值