1.在manifest文件中获取访问网络状态的权限
2.获取网络连接的工具类
int type = 0;
ConnectivityManager connectivitymanager = (ConnectivityManager) context
.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = connectivitymanager.getActiveNetworkInfo();
if (netinfo == null) {
return type;
}
if (netinfo.getType() == ConnectivityManager.TYPE_MOBILE) {
type = 1;
}
if (netinfo.getType() == ConnectivityManager.TYPE_WIFI) {
type = 2;
}
具体的请看demo