android 获取手机网络状态
2017-07-27 14:30
507 查看
与网络相关的,就经常要做网络状态判断及信息获取。 用到的类ConnectivityManager 和NetworkInfo //获取网络连接管理者 ConnectivityManager connectionManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //获取网络的状态信息,有下面三种方式 NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo(); getDetailedState():获取详细状态。 getExtraInfo():获取附加信息。 getReason():获取连接失败的原因。 getType():获取网络类型(一般为移动或Wi-Fi)。 getTypeName():获取网络类型名称(一般取值“WIFI”或“MOBILE”)。 isAvailable():判断该网络是否可用。 isConnected():判断是否已经连接。 isConnectedOrConnecting():判断是否已经连接或正在连接。 isFailover():判断是否连接失败。 isRoaming():判断是否漫游 当用wifi上的时候 getType 是WIFI getExtraInfo是空的 当用手机上的时候 getType 是MOBILE 用移动CMNET方式 getExtraInfo 的值是cmnet 用移动CMWAP方式 getExtraInfo 的值是cmwap 但是不在代理的情况下访问普通的网站访问不了 用联通3gwap方式 getExtraInfo 的值是3gwap 用联通3gnet方式 getExtraInfo 的值是3gnet 用联通uniwap方式 getExtraInfo 的值是uniwap 用联通uninet方式 getExtraInfo 的值是uninet 尝试以下方法获取networktype TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); int networkType = telephonyManager.getNetworkType(); Log.e("network Type", networkType + ""); Log.e("network CountryIso", telephonyManager.getNetworkCountryIso()); Log.e("network Operator", telephonyManager.getNetworkOperator()); Log.e("network OperatorName", telephonyManager.getNetworkOperatorName()); 复制代码 其中: //public static final int NETWORK_TYPE_UNKNOWN = 0; //public static final int NETWORK_TYPE_GPRS = 1; //public static final int NETWORK_TYPE_EDGE = 2; //public static final int NETWORK_TYPE_UMTS = 3; //public static final int NETWORK_TYPE_CDMA = 4; //public static final int NETWORK_TYPE_EVDO_0 = 5; //public static final int NETWORK_TYPE_EVDO_A = 6; //public static final int NETWORK_TYPE_1xRTT = 7; //public static final int NETWORK_TYPE_HSDPA = 8; //public static final int NETWORK_TYPE_HSUPA = 9; //public static final int NETWORK_TYPE_HSPA = 10; //public static final int NETWORK_TYPE_IDEN = 11; 复制代码 下面总结一些常用方法: package com.ltc.demo.networkinfo; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.telephony.TelephonyManager; import android.util.Log; public class NetworkUtil { private static final String TAG = "NetworkUtil"; public static boolean haveInternet(Context context) { NetworkInfo info = (NetworkInfo) ((ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE)) .getActiveNetworkInfo(); if (info == null || !info.isConnected()) { return false; } if (info.isRoaming()) { // here is the roaming option you can change it if you want to // disable internet while roaming, just return false // 是否在漫游,可根据程序需求更改返回值 return false; } return true; } public static boolean isnetWorkAvilable(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); if(connectivityManager == null) { Log.e(TAG, "couldn't get connectivity manager"); } else { NetworkInfo [] networkInfos = connectivityManager.getAllNetworkInfo(); if(networkInfos != null){ for (int i = 0, count = networkInfos.length; i < count; i++) { if(networkInfos[i].getState() == NetworkInfo.State.CONNECTED){ return true; } } } } return false; } public static String getLocalIpAddress() { try { for (Enumeration en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("getLocalIpAddress", ex.toString()); } return null; } public static String getLocalMacAddress(Context context) { WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); } public static boolean isWiFiActive(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) { return true; } } } } return false; } public static boolean hasMoreThanOneConnection(Context context){ ConnectivityManager manager = (ConnectivityManager)context .getSystemService(Context.CONNECTIVITY_SERVICE); if(manager==null){ return false; }else{ NetworkInfo [] info = manager.getAllNetworkInfo(); int counter = 0; for(int i = 0 ;i if(info[i].isConnected()){ counter++; } } if(counter>1){ return true; } } return false; } public static final int NETWORK_TYPE_EHRPD=14; // Level 11 public static final int NETWORK_TYPE_EVDO_B=12; // Level 9 public static final int NETWORK_TYPE_HSPAP=15; // Level 13 public static final int NETWORK_TYPE_IDEN=11; // Level 8 public static final int NETWORK_TYPE_LTE=13; // Level 11 public static boolean isConnected(Context context){ ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); return (info != null && info.isConnected()); } public static boolean isConnectedFast(Context context){ ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); return (info != null && info.isConnected() && isConnectionFast(info.getType(),info.getSubtype())); } public static boolean isConnectionFast(int type, int subType){ if(type==ConnectivityManager.TYPE_WIFI){ System.out.println("CONNECTED VIA WIFI"); return true; }else if(type==ConnectivityManager.TYPE_MOBILE){ switch(subType){ case TelephonyManager.NETWORK_TYPE_1xRTT: return false; // ~ 50-100 kbps case TelephonyManager.NETWORK_TYPE_CDMA: return false; // ~ 14-64 kbps case TelephonyManager.NETWORK_TYPE_EDGE: return false; // ~ 50-100 kbps case TelephonyManager.NETWORK_TYPE_EVDO_0: return true; // ~ 400-1000 kbps case TelephonyManager.NETWORK_TYPE_EVDO_A: return true; // ~ 600-1400 kbps case TelephonyManager.NETWORK_TYPE_GPRS: return false; // ~ 100 kbps case TelephonyManager.NETWORK_TYPE_HSDPA: return true; // ~ 2-14 Mbps case TelephonyManager.NETWORK_TYPE_HSPA: return true; // ~ 700-1700 kbps case TelephonyManager.NETWORK_TYPE_HSUPA: return true; // ~ 1-23 Mbps case TelephonyManager.NETWORK_TYPE_UMTS: return true; // ~ 400-7000 kbps // NOT AVAILABLE YET IN API LEVEL 7 case NETWORK_TYPE_EHRPD: return true; // ~ 1-2 Mbps case NETWORK_TYPE_EVDO_B: return true; // ~ 5 Mbps case NETWORK_TYPE_HSPAP: return true; // ~ 10-20 Mbps case NETWORK_TYPE_IDEN: return false; // ~25 kbps case NETWORK_TYPE_LTE: return true; // ~ 10+ Mbps // Unknown case TelephonyManager.NETWORK_TYPE_UNKNOWN: return false; default: return false; } }else{ return false; } } public static long ip2int(String ip) { String[] items = ip.split("\\."); return Long.valueOf(items[0]) << 24 | Long.valueOf(items[1]) << 16 | Long.valueOf(items[2]) << 8 | Long.valueOf(items[3]); } public static String int2ip(long ipInt) { StringBuilder sb = new StringBuilder(); sb.append(ipInt & 0xFF).append("."); sb.append((ipInt >> 8) & 0xFF).append("."); sb.append((ipInt >> 16) & 0xFF).append("."); sb.append((ipInt >> 24) & 0xFF); return sb.toString(); } } 转载自 http://m.blog.csdn.net/blog/javaswitch/8541607 ---------------------------------------- 1、获取运营商名字 private void getOperatorName() { String operator = telephonyManager.getSimOperator(); if (operator != null) { if (operator.equals("46000") || operator.equals("46002")) { // operatorName="中国移动"; signalTextView.setText("中国移动"); // Toast.makeText(this, "此卡属于(中国移动)", // Toast.LENGTH_SHORT).show(); } else if (operator.equals("46001")) { // operatorName="中国联通"; signalTextView.setText("中国联通"); // Toast.makeText(this, "此卡属于(中国联通)", // Toast.LENGTH_SHORT).show(); } else if (operator.equals("46003")) { // operatorName="中国电信"; signalTextView.setText("中国电信"); // Toast.makeText(this, "此卡属于(中国电信)", // Toast.LENGTH_SHORT).show(); } } } 2 获取网络类型 private void getNetType() { int networkType = telephonyManager.getNetworkType(); if (networkType == TelephonyManager.NETWORK_TYPE_UMTS || networkType == TelephonyManager.NETWORK_TYPE_HSDPA || networkType == TelephonyManager.NETWORK_TYPE_EVDO_0 || networkType == TelephonyManager.NETWORK_TYPE_EVDO_A) { / 联通3G netTypeTextView.setText("3G");、 } else if (networkType == TelephonyManager.NETWORK_TYPE_GPRS || networkType == TelephonyManager.NETWORK_TYPE_EDGE || networkType == TelephonyManager.NETWORK_TYPE_CDMA) { // 2g netTypeTextView.setText("2G"); } else { netTypeTextView.setText(""); } } 转载自 http://blog.csdn.net/crazychickone/article/details/8280930 ---------------------------------------------------- 判断网络类型是wifi,还是3G,还是2G网络,对不同 的网络进行不同的处理,现将判断方法整理给大家,以供参考 说明:下面用到的数据移动2G,联通2G,联通3G,wifi我都已经测试过,暂时手上 没有电信的卡,所以没有验证,有电信手机的同事,可以验证一下,验证后将结果 发送给大家。 ConnectivityManager connectMgr = (ConnectivityManager) this .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = connectMgr.getActiveNetworkInfo(); 一、判断网络是否是wifi,在判断之前一定要进行的非空判断,如果没有任何网络 连接info ==null info.getType() == ConnectivityManager.TYPE_WIFI 二、判断是否是手机网络 info !=null && info.getType() == ConnectivityManager.TYPE_MOBILE 手机网络进行详细区分: info.getSubtype() 这里使用 getSubtype(),不是 getType(),getType()返回的 是0,或者1,是区分是手机网络还是wifi info.getSubtype()取值列表如下: * NETWORK_TYPE_CDMA 网络类型为CDMA * NETWORK_TYPE_EDGE 网络类型为EDGE * NETWORK_TYPE_EVDO_0 网络类型为EVDO0 * NETWORK_TYPE_EVDO_A 网络类型为EVDOA * NETWORK_TYPE_GPRS 网络类型为GPRS * NETWORK_TYPE_HSDPA 网络类型为HSDPA * NETWORK_TYPE_HSPA 网络类型为HSPA * NETWORK_TYPE_HSUPA 网络类型为HSUPA * NETWORK_TYPE_UMTS 网络类型为UMTS 联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信 的3G为EVDO 转载自 http://blog.csdn.net/xxxsz/article/details/8199031 ---------------------------------------------------------------- Android中与电话功能相关的类是 TelephonyManager ,此类中定义了很多常量,以下分类说明 获取以下信息需要在AndroidManifest.xml中指定权限 一、 数据连接状态 获取数据连接状态:int getDataState() 获取数据活动状态:int getDataActivity() 常用的有这几个: int DATA_ACTIVITY_IN 数据连接状态:活动,正在接受数据 int DATA_ACTIVITY_OUT 数据连接状态:活动,正在发送数据 int DATA_ACTIVITY_INOUT 数据连接状态:活动,正在接受和发送数据 int DATA_ACTIVITY_NONE 数据连接状态:活动,但无数据发送和接受 int DATA_CONNECTED 数据连接状态:已连接 int DATA_CONNECTING 数据连接状态:正在连接 int DATA_DISCONNECTED 数据连接状态:断开 int DATA_SUSPENDED 数据连接状态:暂停 二、 移动网络类型 获取网络类型:int getNetworkType() 常用的有这几个: int NETWORK_TYPE_CDMA 网络类型为CDMA int NETWORK_TYPE_EDGE 网络类型为EDGE int NETWORK_TYPE_EVDO_0 网络类型为EVDO0 int NETWORK_TYPE_EVDO_A 网络类型为EVDOA int NETWORK_TYPE_GPRS 网络类型为GPRS int NETWORK_TYPE_HSDPA 网络类型为HSDPA int NETWORK_TYPE_HSPA 网络类型为HSPA int NETWORK_TYPE_HSUPA 网络类型为HSUPA int NETWORK_TYPE_UMTS 网络类型为UMTS 在中国,联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO 三、 手机制式类型 获取手机制式:int getPhoneType() int PHONE_TYPE_CDMA 手机制式为CDMA,电信 int PHONE_TYPE_GSM 手机制式为GSM,移动和联通 int PHONE_TYPE_NONE 手机制式未知 四、 SIM卡状态 获取SIM卡状态:int getSimState() int SIM_STATE_ABSENT SIM卡未找到 int SIM_STATE_NETWORK_LOCKED SIM卡网络被锁定,需要Network PIN解锁 int SIM_STATE_PIN_REQUIRED SIM卡PIN被锁定,需要User PIN解锁 int SIM_STATE_PUK_REQUIRED SIM卡PUK被锁定,需要User PUK解锁 int SIM_STATE_READY SIM卡可用 int SIM_STATE_UNKNOWN SIM卡未知 五、其它信息 String getSimCountryIso() 返回SIM卡提供商的国家代码 String getNetworkCountryIso() 返回ISO标准的国家码,即国际长途区号 String getSimOperator() String getNetworkOperator() 返回MCC+MNC代码 (SIM卡运营商国家代码和运营商网络代码)(IMSI) String getSimOperatorName() String getNetworkOperatorName() 返回移动网络运营商的名字(SPN) String getSubscriberId() 返回IMSI,即国际移动用户识别码 String getDeviceId() 如果是GSM网络,返回IMEI;如果是CDMA网络,返回MEID String getSimSerialNumber() 返回SIM卡的序列号(IMEI) String getLine1Number() 返回手机号码,对于GSM网络来说即MSISDN boolean isNetworkRoaming() 返回手机是否处于漫游状态 -----------附带其它相关信息获取------------------------------ TelephonyManager telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); /**手机串号:GSM手机的 IMEI 和 CDMA手机的 MEID.*/ String deviceID =telephonyManager.getDeviceId(); /**获取手机SIM卡的序列号*/ String imei =telephonyManager.getSimSerialNumber(); /**电话方位*/ CellLocation str =telephonyManager.getCellLocation(); /**取得和语音邮件相关的标签,即为识别符*/ String voiceMail =telephonyManager.getVoiceMailAlphaTag(); /**获取语音邮件号码*/ String voiceMailNumber =telephonyManager.getVoiceMailNumber(); /**获取ISO国家码,相当于提供SIM卡的国家码*/ String simCountryIso =telephonyManager.getSimCountryIso(); 解释: IMSI是国际移动用户识别码的简称(International Mobile Subscriber Identity) IMSI共有15位,其结构如下: MCC+MNC+MIN MCC:Mobile Country Code,移动国家码,共3位,中国为460; MNC:Mobile NetworkCode,移动网络码,共2位 在中国,移动的代码为电00和02,联通的代码为01,电信的代码为03 合起来就是(也是Android手机中APN配置文件中的代码): 中国移动:46000 46002 中国联通:46001 中国电信:46003 举例,一个典型的IMSI号码为460030912121001 IMEI是International Mobile Equipment Identity (国际移动设备标识)的简称 IMEI由15位数字组成的”电子串号”,它与每台手机一一对应,而且该码是全世界唯一的 其组成为: 1. 前6位数(TAC)是”型号核准号码”,一般代表机型 2. 接着的2位数(FAC)是”最后装配号”,一般代表产地 3. 之后的6位数(SNR)是”串号”,一般代表生产顺序号 4. 最后1位数(SP)通常是”0″,为检验码,目前暂备用 转载自 http://www.cnblogs.com/tangxl/articles/2025506.html[/code]
相关文章推荐
- 朝花夕拾-android 获取当前手机的内存卡状态和网络连接状态
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android如何获取当前手机的网络状态
- 获取和改变Android手机网络状态的代码实现
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- android获取手机的网络状态
- android获取手机的网络状态
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- android 获取手机的网络状态
- Android开发之获取手机网络状态及网络是否连接
- android 获取手机网络状态
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android获取手机状态和监听手机来电状态
- ios如何获取手机的网络状态和运营商名称
- 获取Android手机中SIM卡状态
- TelephonyManager类:Android手机及Sim卡状态的获取
- Determining and Monitoring the Connectivity StatusAndroid 电源管理专题之获取和监测网络连接状态
- Android 判断手机当前的网络状态
- 获取Android当前网络类型(2g、3g、4g、wifi)以及手机型号、版本号