当前位置: 移动技术网 > IT编程>移动开发>Android > Android中判断有无可用网络的代码(是否是3G或者WIFI网络)

Android中判断有无可用网络的代码(是否是3G或者WIFI网络)

2019年07月24日  | 移动技术网IT编程  | 我要评论

hookport.sys,超能狗国语版,113003

复制代码 代码如下:

connectivitymanager mconnectivity = (connectivitymanager)getsystemservice(context.connectivity_service);
telephonymanager mtelephony = (telephonymanager)this.getsystemservice(telephony_service);
//检查网络连接,如果无网络可用,就不需要进行连网操作等
networkinfo info = mconnectivity.getactivenetworkinfo();

if (info == null || !mconnectivity.getbackgrounddatasetting()) {
return false;
}

//判断网络连接类型,只有在3g或wifi里进行一些数据更新。
int nettype = info.gettype();
int netsubtype = info.getsubtype();

if (nettype == connectivitymanager.type_wifi) {
return info.isconnected();
} else if (nettype == connectivitymanager.type_mobile
&& netsubtype == telephonymanager.network_type_umts
&& !mtelephony.isnetworkroaming()) {
return info.isconnected();
} else {
return false;
}

别忘了在 androidmanifest.xml 中加上 检查网络的权限

<uses-permission android:name="android.permission.access_network_state"/>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网