Android判断上网方式(Wifi还是数据流量)
2017-08-07 17:33
519 查看
原文地址:http://blog.csdn.net/xundh/article/details/44916177
首先要在AndroidManifest.xml加上权限:
[html] view
plain copy
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
判断有无网络连接:
[java] view
plain copy
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;
}
检查网络类型:
[java] view
plain copy
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) { //WIFI
return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { //MOBILE
return info.isConnected();
} else {
return false;
}
判断WiFi是否已连接:
[java] view
plain copy
/**
* make true current connect service is wifi
* @param mContext
* @return
*/
private static boolean isWifi(Context mContext) {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
判断WiFi和移动流量是否已连接:
[java] view
plain copy
public static boolean checkNetworkConnection(Context context)
{
final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifi.isAvailable()||mobile.isAvailable()) //getState()方法是查询是否连接了数据网络
return true;
else
return false;
}
只判断移动网络连接是否正常:
[java] view
plain copy
<span style="font-family: Arial, Helvetica, sans-serif;">public boolean isMobileConnected(Context context) {
</span><span style="font-family: Arial, Helvetica, sans-serif;"> if (context != null) { </span>
[java] view
plain copy
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); //获取移动网络信息
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable(); //getState()方法是查询是否连接了数据网络
}
}
return false;
首先要在AndroidManifest.xml加上权限:
[html] view
plain copy
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
判断有无网络连接:
[java] view
plain copy
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;
}
检查网络类型:
[java] view
plain copy
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) { //WIFI
return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { //MOBILE
return info.isConnected();
} else {
return false;
}
判断WiFi是否已连接:
[java] view
plain copy
/**
* make true current connect service is wifi
* @param mContext
* @return
*/
private static boolean isWifi(Context mContext) {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
判断WiFi和移动流量是否已连接:
[java] view
plain copy
public static boolean checkNetworkConnection(Context context)
{
final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifi.isAvailable()||mobile.isAvailable()) //getState()方法是查询是否连接了数据网络
return true;
else
return false;
}
只判断移动网络连接是否正常:
[java] view
plain copy
<span style="font-family: Arial, Helvetica, sans-serif;">public boolean isMobileConnected(Context context) {
</span><span style="font-family: Arial, Helvetica, sans-serif;"> if (context != null) { </span>
[java] view
plain copy
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); //获取移动网络信息
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable(); //getState()方法是查询是否连接了数据网络
}
}
return false;
相关文章推荐
- Android判断上网方式(Wifi还是数据流量)
- Android判断上网方式(Wifi还是数据流量)
- 安卓简单技术-判断手机连接的是wifi还是数据流量
- Android判断手机是数据连接还是wifi连接
- android 判断网络状态 (无网络 wifi 数据流量)
- Android判断Wifi高级设置是静态IP配置方式还是DHCP配置方式
- Android中wifi与数据流量的切换监听
- Android之判断手机连接的网络类型是WIFI还是2G/3G/4G
- Android判断网络连接是否可用,WiFi、移动数据是否打开等
- Android中用对话框打开wifi和数据流量。
- android 判断是wifi还是4g网络
- Android中wifi与数据流量的切换监听详解
- android 解析服务器数据使用json还是xml方式
- [置顶] Android开发之强大的网络判断工具,判断是否联网,判断是wifi还是3g网络等java工具代码类
- Android 数据流量(3G,4G)与wifi 转换实时监听
- android 移动数据流量打开导致获取wifi热点IP错误
- Android判断wifi,GPRS数据连接
- Android:判断当前网络是WIFI还是手机网络
- Android判断是Wifi还是4G网络代码
- Android 5.1改进后 可判断wifi是否上网