Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
来源:互联网 发布:软件实训心得体会 编辑:程序博客网 时间:2024/09/21 08:55
不多说了,直接看代码, NB人会懂的!
package com.example.vdisktest;import android.app.Activity;import android.content.Context;import android.database.Cursor;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.net.Uri;import android.os.Bundle;import android.telephony.TelephonyManager;import android.text.TextUtils;import android.util.Log;public class NetTypeActivity extends Activity {/** Called when the activity is first created. */public static final String CTWAP = "ctwap";public static final String CTNET = "ctnet";public static final String CMWAP = "cmwap";public static final String CMNET = "cmnet";public static final String NET_3G = "3gnet";public static final String WAP_3G = "3gwap";public static final String UNIWAP = "uniwap";public static final String UNINET = "uninet";public static final int TYPE_CT_WAP = 5;public static final int TYPE_CT_NET = 6;public static final int TYPE_CT_WAP_2G = 7;public static final int TYPE_CT_NET_2G = 8;public static final int TYPE_CM_WAP = 9;public static final int TYPE_CM_NET = 10;public static final int TYPE_CM_WAP_2G = 11;public static final int TYPE_CM_NET_2G = 12;public static final int TYPE_CU_WAP = 13;public static final int TYPE_CU_NET = 14;public static final int TYPE_CU_WAP_2G = 15;public static final int TYPE_CU_NET_2G = 16;public static final int TYPE_OTHER = 17;public static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");/** 没有网络 */public static final int TYPE_NET_WORK_DISABLED = 0;/** wifi网络 */public static final int TYPE_WIFI = 4;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);long start = System.currentTimeMillis();int checkNetworkType = checkNetworkType(this);Log.i("NetType", "===========elpase:"+ (System.currentTimeMillis() - start));switch (checkNetworkType) {case TYPE_WIFI:Log.i("NetType", "================wifi");break;case TYPE_NET_WORK_DISABLED:Log.i("NetType", "================no network");break;case TYPE_CT_WAP:Log.i("NetType", "================ctwap");break;case TYPE_CT_WAP_2G:Log.i("NetType", "================ctwap_2g");break;case TYPE_CT_NET:Log.i("NetType", "================ctnet");break;case TYPE_CT_NET_2G:Log.i("NetType", "================ctnet_2g");break;case TYPE_CM_WAP:Log.i("NetType", "================cmwap");break;case TYPE_CM_WAP_2G:Log.i("NetType", "================cmwap_2g");break;case TYPE_CM_NET:Log.i("NetType", "================cmnet");break;case TYPE_CM_NET_2G:Log.i("NetType", "================cmnet_2g");break;case TYPE_CU_NET:Log.i("NetType", "================cunet");break;case TYPE_CU_NET_2G:Log.i("NetType", "================cunet_2g");break;case TYPE_CU_WAP:Log.i("NetType", "================cuwap");break;case TYPE_CU_WAP_2G:Log.i("NetType", "================cuwap_2g");break;case TYPE_OTHER:Log.i("NetType", "================other");break;default:break;}}/*** * 判断Network具体类型(联通移动wap,电信wap,其他net) * * */public static int checkNetworkType(Context mContext) {try {final ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);final NetworkInfo mobNetInfoActivity = connectivityManager.getActiveNetworkInfo();if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) {// 注意一:// NetworkInfo 为空或者不可以用的时候正常情况应该是当前没有可用网络,// 但是有些电信机器,仍可以正常联网,// 所以当成net网络处理依然尝试连接网络。// (然后在socket中捕捉异常,进行二次判断与用户提示)。return TYPE_NET_WORK_DISABLED;} else {// NetworkInfo不为null开始判断是网络类型int netType = mobNetInfoActivity.getType();if (netType == ConnectivityManager.TYPE_WIFI) {// wifi net处理return TYPE_WIFI;} else if (netType == ConnectivityManager.TYPE_MOBILE) {// 注意二:// 判断是否电信wap:// 不要通过getExtraInfo获取接入点名称来判断类型,// 因为通过目前电信多种机型测试发现接入点名称大都为#777或者null,// 电信机器wap接入点中要比移动联通wap接入点多设置一个用户名和密码,// 所以可以通过这个进行判断!boolean is3G = isFastMobileNetwork(mContext);final Cursor c = mContext.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);if (c != null) {c.moveToFirst();final String user = c.getString(c.getColumnIndex("user"));if (!TextUtils.isEmpty(user)) {if (user.startsWith(CTWAP)) {return is3G ? TYPE_CT_WAP : TYPE_CT_WAP_2G;} else if (user.startsWith(CTNET)) {return is3G ? TYPE_CT_NET : TYPE_CT_NET_2G;}}}c.close();// 注意三:// 判断是移动联通wap:// 其实还有一种方法通过getString(c.getColumnIndex("proxy")获取代理ip// 来判断接入点,10.0.0.172就是移动联通wap,10.0.0.200就是电信wap,但在// 实际开发中并不是所有机器都能获取到接入点代理信息,例如魅族M9 (2.2)等...// 所以采用getExtraInfo获取接入点名字进行判断String netMode = mobNetInfoActivity.getExtraInfo();Log.i("", "==================netmode:" + netMode);if (netMode != null) {// 通过apn名称判断是否是联通和移动wapnetMode = netMode.toLowerCase();if (netMode.equals(CMWAP)) {return is3G ? TYPE_CM_WAP : TYPE_CM_WAP_2G;} else if (netMode.equals(CMNET)) {return is3G ? TYPE_CM_NET : TYPE_CM_NET_2G;} else if (netMode.equals(NET_3G)|| netMode.equals(UNINET)) {return is3G ? TYPE_CU_NET : TYPE_CU_NET_2G;} else if (netMode.equals(WAP_3G)|| netMode.equals(UNIWAP)) {return is3G ? TYPE_CU_WAP : TYPE_CU_WAP_2G;}}}}} catch (Exception ex) {ex.printStackTrace();return TYPE_OTHER;}return TYPE_OTHER;}private static boolean isFastMobileNetwork(Context context) {TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);switch (telephonyManager.getNetworkType()) {case TelephonyManager.NETWORK_TYPE_1xRTT:return false; // ~ 50-100 kbpscase TelephonyManager.NETWORK_TYPE_CDMA:return false; // ~ 14-64 kbpscase TelephonyManager.NETWORK_TYPE_EDGE:return false; // ~ 50-100 kbpscase TelephonyManager.NETWORK_TYPE_EVDO_0:return true; // ~ 400-1000 kbpscase TelephonyManager.NETWORK_TYPE_EVDO_A:return true; // ~ 600-1400 kbpscase TelephonyManager.NETWORK_TYPE_GPRS:return false; // ~ 100 kbpscase TelephonyManager.NETWORK_TYPE_HSDPA:return true; // ~ 2-14 Mbpscase TelephonyManager.NETWORK_TYPE_HSPA:return true; // ~ 700-1700 kbpscase TelephonyManager.NETWORK_TYPE_HSUPA:return true; // ~ 1-23 Mbpscase TelephonyManager.NETWORK_TYPE_UMTS:return true; // ~ 400-7000 kbpscase TelephonyManager.NETWORK_TYPE_EHRPD:return true; // ~ 1-2 Mbpscase TelephonyManager.NETWORK_TYPE_EVDO_B:return true; // ~ 5 Mbpscase TelephonyManager.NETWORK_TYPE_HSPAP:return true; // ~ 10-20 Mbpscase TelephonyManager.NETWORK_TYPE_IDEN:return false; // ~25 kbpscase TelephonyManager.NETWORK_TYPE_LTE:return true; // ~ 10+ Mbpscase TelephonyManager.NETWORK_TYPE_UNKNOWN:return false;default:return false;}}}
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android之判断手机连接的网络类型是WIFI还是2G/3G/4G
- Android之判断手机连接的网络类型是WIFI还是2G/3G/4G
- Android之判断手机连接的网络类型是WIFI还是2G/3G/4G
- Android之判断手机连接的网络类型是WIFI还是2G/3G/4G(转载)
- android代码判断当前手机的手机号码是属于移动、联通、还是电信的
- android 判断当前联网类型是2g,3g,4g,wifi还是其他
- Android中,判断是否联网,连接的是移动数据还是WiFi,移动数据是2g,3g,还是4g
- android手机网络状态监听,判断手机连接的是移动网络还是wifi等
- 获取手机品牌机型 手机运营商 以及数据流量类型 是2G还是3G,4G 和 获取手机当前网络类型
- 关于获取当前网络是属于2G还是3G(极其简单)
- 关于获取当前网络是属于2G还是3G(极其简单)
- 关于获取当前网络是属于2G还是3G(极其简单)
- 关于获取当前网络是属于2G还是3G(极其简单)
- 检查手机网络状态是关网还是GPRS 还是WIFI
- Linux常用的网络命令
- mybatis iterate
- 数据库的增量备份和差异备份的区别
- 归并排序求逆序对数
- 关闭cpu自动降频
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- 经典论文翻译导读之《Google File System》
- 离散化+树状数组 求逆序对数
- MYSQL性能优化
- android GridView布局
- MongoDB的聚合函数 Aggregate
- 通过 GCC 学习 OpenMP 框架
- ArcGIS制作鹰眼
- Session会话监听程序