• Android 判断用户2G/3G/4G移动数据网络


    Android 判断用户2G/3G/4G移动数据网络

    在做 Android App 的时候,为了给用户省流量,为了不激起用户的愤怒,为了更好的用户体验,是需要根据用户当前网络情况来做一些调整的,也可以在 App 的设置模块里,让用户自己选择,在 2G / 3G / 4G 网络条件下,是否允许请求一些流量比较大的数据。

    通过 Android 提供的 TelephonyManager 和 ConnectivityManager 都可以获取到 NetworksInfo 对象,可以通过 getType() 获取类型,判断是 wifi 还是 mobile ,如果是 mobile ,可以通过 NetworksInfo 对象的 getSubType() 和 getSubTypeName() 可以获取到对于的网络类型和名字。

    网络类型和名字定义在 TelephonyManager 类里。

     1 /** Network type is unknown */
     2 public static final int NETWORK_TYPE_UNKNOWN = 0;
     3 /** Current network is GPRS */
     4 public static final int NETWORK_TYPE_GPRS = 1;
     5 /** Current network is EDGE */
     6 public static final int NETWORK_TYPE_EDGE = 2;
     7 /** Current network is UMTS */
     8 public static final int NETWORK_TYPE_UMTS = 3;
     9 /** Current network is CDMA: Either IS95A or IS95B*/
    10 public static final int NETWORK_TYPE_CDMA = 4;
    11 /** Current network is EVDO revision 0*/
    12 public static final int NETWORK_TYPE_EVDO_0 = 5;
    13 /** Current network is EVDO revision A*/
    14 public static final int NETWORK_TYPE_EVDO_A = 6;
    15 /** Current network is 1xRTT*/
    16 public static final int NETWORK_TYPE_1xRTT = 7;
    17 /** Current network is HSDPA */
    18 public static final int NETWORK_TYPE_HSDPA = 8;
    19 /** Current network is HSUPA */
    20 public static final int NETWORK_TYPE_HSUPA = 9;
    21 /** Current network is HSPA */
    22 public static final int NETWORK_TYPE_HSPA = 10;
    23 /** Current network is iDen */
    24 public static final int NETWORK_TYPE_IDEN = 11;
    25 /** Current network is EVDO revision B*/
    26 public static final int NETWORK_TYPE_EVDO_B = 12;
    27 /** Current network is LTE */
    28 public static final int NETWORK_TYPE_LTE = 13;
    29 /** Current network is eHRPD */
    30 public static final int NETWORK_TYPE_EHRPD = 14;
    31 /** Current network is HSPA+ */
    32 public static final int NETWORK_TYPE_HSPAP = 15;

    看到这个代码和注释,相信没有这方面知识的人很难看懂,都啥玩意?这注释跟没注释有啥区别?!就是让人看着更加闹心而已。所以说,注释对阅读代码的人很重 要。当然这些东西可能太专业了,写这些代码的人估计是想写也不知道该怎么了,得写多大一坨啊?!我在最后会贴上一些我整理的资料,可以供大家参考一下,不 是很详细,也不专业,就是大概有个印象。

    TelephonyManager 还提供了 getNetworkTypeName(int type) 的方法,这个方法可以返回一个字符串,但是信息量不大。

    那怎么判断是 2G , 3G 还是 4G 网络呢?TelephonyManager 还提供了另外一个方法,getNetworkClass(int networkType) ,但这个方法被隐藏掉了,我把代码贴一下。

    public static int getNetworkClass(int networkType) { 
    switch (networkType) { 
    case NETWORK_TYPE_GPRS: 
    case NETWORK_TYPE_EDGE: 
    case NETWORK_TYPE_CDMA: 
    case NETWORK_TYPE_1xRTT: 
    case NETWORK_TYPE_IDEN: 
    return NETWORK_CLASS_2_G; 
    case NETWORK_TYPE_UMTS: 
    case NETWORK_TYPE_EVDO_0: 
    case NETWORK_TYPE_EVDO_A: 
    case NETWORK_TYPE_HSDPA: 
    case NETWORK_TYPE_HSUPA: 
    case NETWORK_TYPE_HSPA: 
    case NETWORK_TYPE_EVDO_B: 
    case NETWORK_TYPE_EHRPD: 
    case NETWORK_TYPE_HSPAP: 
    return NETWORK_CLASS_3_G; 
    case NETWORK_TYPE_LTE: 
    return NETWORK_CLASS_4_G; 
    default: 
    return NETWORK_CLASS_UNKNOWN; 

    然后下面是这几个常量的值。

    /** Unknown network class. {@hide} */ 
    public static final int NETWORK_CLASS_UNKNOWN = 0; 
    /** Class of broadly defined "2G" networks. {@hide} */ 
    public static final int NETWORK_CLASS_2_G = 1; 
    /** Class of broadly defined "3G" networks. {@hide} */ 
    public static final int NETWORK_CLASS_3_G = 2; 
    /** Class of broadly defined "4G" networks. {@hide} */ 
    public static final int NETWORK_CLASS_4_G = 3; 

    不知道为啥要把这些东西给隐藏起来,然道是不靠谱?!还是其他的更好的方式?!不知道,先这样吧,现在通过上面的手段,是可以知道用户用的是什么网络,当 然也可以区分出来用户使用的是 2G , 3G 还是 4G 了。当然,你获取到这些数据后,你也可以推算出用户用的是哪家公司的网络,移动的,联通的,还是电信的,当然,只在中国。而且虚拟运营商开始真正上市后, 这个就区分不出来是京东的,还是国美,苏宁的了,但是你可以知道你的手机号用的是联通的网还是移动的网。

    最后贴上我收集整理的一些资料,可以参考一下。

    GPRS       2G(2.5) General Packet Radia Service 114kbps
    EDGE       2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
    UMTS      3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
    CDMA     2G 电信 Code Division Multiple Access 码分多址
    EVDO_0   3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
    EVDO_A  3G 1.8mbps - 3.1mbps 属于3G过渡,3.5G
    1xRTT      2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
    HSDPA    3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps 
    HSUPA    3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
    HSPA      3G (分HSDPA,HSUPA) High Speed Packet Access 
    IDEN      2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 (属于2G,来自维基百科)
    EVDO_B 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
    LTE        4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE Advanced 才是4G 
    EHRPD  3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
    HSPAP  3G HSPAP 比 HSDPA 快些

    转自:http://www.binkery.com/post/368.html

    实例:

      1 import java.io.BufferedReader;  
      2 import java.io.InputStreamReader;  
      3 import java.text.DecimalFormat;  
      4 import java.util.List;  
      5    
      6 import android.content.Context;  
      7 import android.net.ConnectivityManager;  
      8 import android.net.NetworkInfo;  
      9 import android.net.wifi.WifiInfo;  
     10 import android.net.wifi.WifiManager;  
     11 import android.telephony.NeighboringCellInfo;  
     12 import android.telephony.TelephonyManager;  
     13 import android.telephony.cdma.CdmaCellLocation;  
     14 import android.telephony.gsm.GsmCellLocation;  
     15 import android.util.Log;  
     16    
     17 public class NetWorkUtil {  
     18    
     19     public static boolean isWifiAvailable() {  
     20         ConnectivityManager connectivityManager = (ConnectivityManager) ConfigManager  
     21                 .getContext().getSystemService(Context.CONNECTIVITY_SERVICE);  
     22         NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();  
     23         return (networkInfo != null && networkInfo.isConnected() && networkInfo  
     24                 .getType() == ConnectivityManager.TYPE_WIFI);  
     25     }  
     26    
     27     /** 
     28      * 获取MAC地址 
     29      *  
     30      * @param context 
     31      * @return 
     32      */ 
     33     public static String getMacAddress(Context context) {  
     34         if (context == null) {  
     35             return "";  
     36         }  
     37    
     38         String localMac = null;  
     39         if (isWifiAvailable()) {  
     40             localMac = getWifiMacAddress(context);  
     41         }  
     42    
     43         if (localMac != null && localMac.length() > 0) {  
     44             localMac = localMac.replace(":", "-").toLowerCase();  
     45             return localMac;  
     46         }  
     47    
     48         localMac = getMacFromCallCmd();  
     49         if (localMac != null) {  
     50             localMac = localMac.replace(":", "-").toLowerCase();  
     51         }  
     52    
     53         return localMac;  
     54     }  
     55    
     56     private static String getWifiMacAddress(Context context) {  
     57         String localMac = null;  
     58         try {  
     59             WifiManager wifi = (WifiManager) context  
     60                     .getSystemService(Context.WIFI_SERVICE);  
     61             WifiInfo info = wifi.getConnectionInfo();  
     62             if (wifi.isWifiEnabled()) {  
     63                 localMac = info.getMacAddress();  
     64                 if (localMac != null) {  
     65                     localMac = localMac.replace(":", "-").toLowerCase();  
     66                     return localMac;  
     67                 }  
     68             }  
     69    
     70         } catch (Exception e) {  
     71             e.printStackTrace();  
     72         }  
     73    
     74         return null;  
     75     }  
     76    
     77     /** 
     78      * 通过callCmd("busybox ifconfig","HWaddr")获取mac地址 
     79      *  
     80      * @attention 需要设备装有busybox工具 
     81      * @return Mac Address 
     82      */ 
     83     private static String getMacFromCallCmd() {  
     84         String result = "";  
     85         result = callCmd("busybox ifconfig", "HWaddr");  
     86    
     87         if (result == null || result.length() <= 0) {  
     88             return null;  
     89         }  
     90    
     91         DebugLog.v("tag", "cmd result : " + result);  
     92    
     93         // 对该行数据进行解析  
     94         // 例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67  
     95         if (result.length() > 0 && result.contains("HWaddr") == true) {  
     96             String Mac = result.substring(result.indexOf("HWaddr") + 6,  
     97                     result.length() - 1);  
     98             if (Mac.length() > 1) {  
     99                 result = Mac.replaceAll(" ", "");  
    100             }  
    101         }  
    102    
    103         return result;  
    104     }  
    105    
    106     public static String callCmd(String cmd, String filter) {  
    107         String result = "";  
    108         String line = "";  
    109         try {  
    110             Process proc = Runtime.getRuntime().exec(cmd);  
    111             InputStreamReader is = new InputStreamReader(proc.getInputStream());  
    112             BufferedReader br = new BufferedReader(is);  
    113    
    114             // 执行命令cmd,只取结果中含有filter的这一行  
    115             while ((line = br.readLine()) != null 
    116                     && line.contains(filter) == false) {  
    117             }  
    118    
    119             result = line;  
    120         } catch (Exception e) {  
    121             e.printStackTrace();  
    122         }  
    123         return result;  
    124     }  
    125    
    126     /** 
    127      * 网络是否可用 
    128      *  
    129      * @param context 
    130      * @return 
    131      */ 
    132     public static boolean IsNetWorkEnable(Context context) {  
    133         try {  
    134             ConnectivityManager connectivity = (ConnectivityManager) context  
    135                     .getSystemService(Context.CONNECTIVITY_SERVICE);  
    136             if (connectivity == null) {  
    137                 ToastUtil.showMessage(context, "无法连接网络");  
    138                 return false;  
    139             }  
    140    
    141             NetworkInfo info = connectivity.getActiveNetworkInfo();  
    142             if (info != null && info.isConnected()) {  
    143                 // 判断当前网络是否已经连接  
    144                 if (info.getState() == NetworkInfo.State.CONNECTED) {  
    145                     return true;  
    146                 }  
    147             }  
    148    
    149         } catch (Exception e) {  
    150             e.printStackTrace();  
    151         }  
    152         ToastUtil.showMessage(context, "无法连接网络");  
    153         return false;  
    154     }  
    155    
    156     private static final int NETWORK_TYPE_UNAVAILABLE = -1;  
    157     // private static final int NETWORK_TYPE_MOBILE = -100;  
    158     private static final int NETWORK_TYPE_WIFI = -101;  
    159    
    160     private static final int NETWORK_CLASS_WIFI = -101;  
    161     private static final int NETWORK_CLASS_UNAVAILABLE = -1;  
    162     /** Unknown network class. */ 
    163     private static final int NETWORK_CLASS_UNKNOWN = 0;  
    164     /** Class of broadly defined "2G" networks. */ 
    165     private static final int NETWORK_CLASS_2_G = 1;  
    166     /** Class of broadly defined "3G" networks. */ 
    167     private static final int NETWORK_CLASS_3_G = 2;  
    168     /** Class of broadly defined "4G" networks. */ 
    169     private static final int NETWORK_CLASS_4_G = 3;  
    170    
    171     private static DecimalFormat df = new DecimalFormat("#.##");  
    172    
    173     // 适配低版本手机  
    174     /** Network type is unknown */ 
    175     public static final int NETWORK_TYPE_UNKNOWN = 0;  
    176     /** Current network is GPRS */ 
    177     public static final int NETWORK_TYPE_GPRS = 1;  
    178     /** Current network is EDGE */ 
    179     public static final int NETWORK_TYPE_EDGE = 2;  
    180     /** Current network is UMTS */ 
    181     public static final int NETWORK_TYPE_UMTS = 3;  
    182     /** Current network is CDMA: Either IS95A or IS95B */ 
    183     public static final int NETWORK_TYPE_CDMA = 4;  
    184     /** Current network is EVDO revision 0 */ 
    185     public static final int NETWORK_TYPE_EVDO_0 = 5;  
    186     /** Current network is EVDO revision A */ 
    187     public static final int NETWORK_TYPE_EVDO_A = 6;  
    188     /** Current network is 1xRTT */ 
    189     public static final int NETWORK_TYPE_1xRTT = 7;  
    190     /** Current network is HSDPA */ 
    191     public static final int NETWORK_TYPE_HSDPA = 8;  
    192     /** Current network is HSUPA */ 
    193     public static final int NETWORK_TYPE_HSUPA = 9;  
    194     /** Current network is HSPA */ 
    195     public static final int NETWORK_TYPE_HSPA = 10;  
    196     /** Current network is iDen */ 
    197     public static final int NETWORK_TYPE_IDEN = 11;  
    198     /** Current network is EVDO revision B */ 
    199     public static final int NETWORK_TYPE_EVDO_B = 12;  
    200     /** Current network is LTE */ 
    201     public static final int NETWORK_TYPE_LTE = 13;  
    202     /** Current network is eHRPD */ 
    203     public static final int NETWORK_TYPE_EHRPD = 14;  
    204     /** Current network is HSPA+ */ 
    205     public static final int NETWORK_TYPE_HSPAP = 15;  
    206    
    207     /** 
    208      * 格式化大小 
    209      *  
    210      * @param size 
    211      * @return 
    212      */ 
    213     public static String formatSize(long size) {  
    214         String unit = "B";  
    215         float len = size;  
    216         if (len > 900) {  
    217             len /= 1024f;  
    218             unit = "KB";  
    219         }  
    220         if (len > 900) {  
    221             len /= 1024f;  
    222             unit = "MB";  
    223         }  
    224         if (len > 900) {  
    225             len /= 1024f;  
    226             unit = "GB";  
    227         }  
    228         if (len > 900) {  
    229             len /= 1024f;  
    230             unit = "TB";  
    231         }  
    232         return df.format(len) + unit;  
    233     }  
    234    
    235     public static String formatSizeBySecond(long size) {  
    236         String unit = "B";  
    237         float len = size;  
    238         if (len > 900) {  
    239             len /= 1024f;  
    240             unit = "KB";  
    241         }  
    242         if (len > 900) {  
    243             len /= 1024f;  
    244             unit = "MB";  
    245         }  
    246         if (len > 900) {  
    247             len /= 1024f;  
    248             unit = "GB";  
    249         }  
    250         if (len > 900) {  
    251             len /= 1024f;  
    252             unit = "TB";  
    253         }  
    254         return df.format(len) + unit + "/s";  
    255     }  
    256    
    257     public static String format(long size) {  
    258         String unit = "B";  
    259         float len = size;  
    260         if (len > 1000) {  
    261             len /= 1024f;  
    262             unit = "KB";  
    263             if (len > 1000) {  
    264                 len /= 1024f;  
    265                 unit = "MB";  
    266                 if (len > 1000) {  
    267                     len /= 1024f;  
    268                     unit = "GB";  
    269                 }  
    270             }  
    271         }  
    272         return df.format(len) + "
    " + unit + "/s";  
    273     }  
    274    
    275     /** 
    276      * 获取运营商 
    277      *  
    278      * @return 
    279      */ 
    280     public static String getProvider() {  
    281         String provider = "未知";  
    282         try {  
    283             TelephonyManager telephonyManager = (TelephonyManager) ConfigManager  
    284                     .getContext().getSystemService(Context.TELEPHONY_SERVICE);  
    285             String IMSI = telephonyManager.getSubscriberId();  
    286             Log.v("tag", "getProvider.IMSI:" + IMSI);  
    287             if (IMSI == null) {  
    288                 if (TelephonyManager.SIM_STATE_READY == telephonyManager  
    289                         .getSimState()) {  
    290                     String operator = telephonyManager.getSimOperator();  
    291                     Log.v("tag", "getProvider.operator:" + operator);  
    292                     if (operator != null) {  
    293                         if (operator.equals("46000")  
    294                                 || operator.equals("46002")  
    295                                 || operator.equals("46007")) {  
    296                             provider = "中国移动";  
    297                         } else if (operator.equals("46001")) {  
    298                             provider = "中国联通";  
    299                         } else if (operator.equals("46003")) {  
    300                             provider = "中国电信";  
    301                         }  
    302                     }  
    303                 }  
    304             } else {  
    305                 if (IMSI.startsWith("46000") || IMSI.startsWith("46002")  
    306                         || IMSI.startsWith("46007")) {  
    307                     provider = "中国移动";  
    308                 } else if (IMSI.startsWith("46001")) {  
    309                     provider = "中国联通";  
    310                 } else if (IMSI.startsWith("46003")) {  
    311                     provider = "中国电信";  
    312                 }  
    313             }  
    314         } catch (Exception e) {  
    315             e.printStackTrace();  
    316         }  
    317         return provider;  
    318     }  
    319    
    320     /** 
    321      * 获取网络类型 
    322      *  
    323      * @return 
    324      */ 
    325     public static String getCurrentNetworkType() {  
    326         int networkClass = getNetworkClass();  
    327         String type = "未知";  
    328         switch (networkClass) {  
    329         case NETWORK_CLASS_UNAVAILABLE:  
    330             type = "无";  
    331             break;  
    332         case NETWORK_CLASS_WIFI:  
    333             type = "Wi-Fi";  
    334             break;  
    335         case NETWORK_CLASS_2_G:  
    336             type = "2G";  
    337             break;  
    338         case NETWORK_CLASS_3_G:  
    339             type = "3G";  
    340             break;  
    341         case NETWORK_CLASS_4_G:  
    342             type = "4G";  
    343             break;  
    344         case NETWORK_CLASS_UNKNOWN:  
    345             type = "未知";  
    346             break;  
    347         }  
    348         return type;  
    349     }  
    350    
    351     private static int getNetworkClassByType(int networkType) {  
    352         switch (networkType) {  
    353         case NETWORK_TYPE_UNAVAILABLE:  
    354             return NETWORK_CLASS_UNAVAILABLE;  
    355         case NETWORK_TYPE_WIFI:  
    356             return NETWORK_CLASS_WIFI;  
    357         case NETWORK_TYPE_GPRS:  
    358         case NETWORK_TYPE_EDGE:  
    359         case NETWORK_TYPE_CDMA:  
    360         case NETWORK_TYPE_1xRTT:  
    361         case NETWORK_TYPE_IDEN:  
    362             return NETWORK_CLASS_2_G;  
    363         case NETWORK_TYPE_UMTS:  
    364         case NETWORK_TYPE_EVDO_0:  
    365         case NETWORK_TYPE_EVDO_A:  
    366         case NETWORK_TYPE_HSDPA:  
    367         case NETWORK_TYPE_HSUPA:  
    368         case NETWORK_TYPE_HSPA:  
    369         case NETWORK_TYPE_EVDO_B:  
    370         case NETWORK_TYPE_EHRPD:  
    371         case NETWORK_TYPE_HSPAP:  
    372             return NETWORK_CLASS_3_G;  
    373         case NETWORK_TYPE_LTE:  
    374             return NETWORK_CLASS_4_G;  
    375         default:  
    376             return NETWORK_CLASS_UNKNOWN;  
    377         }  
    378     }  
    379    
    380     private static int getNetworkClass() {  
    381         int networkType = NETWORK_TYPE_UNKNOWN;  
    382         try {  
    383             final NetworkInfo network = ((ConnectivityManager) ConfigManager  
    384                     .getContext()  
    385                     .getSystemService(Context.CONNECTIVITY_SERVICE))  
    386                     .getActiveNetworkInfo();  
    387             if (network != null && network.isAvailable()  
    388                     && network.isConnected()) {  
    389                 int type = network.getType();  
    390                 if (type == ConnectivityManager.TYPE_WIFI) {  
    391                     networkType = NETWORK_TYPE_WIFI;  
    392                 } else if (type == ConnectivityManager.TYPE_MOBILE) {  
    393                     TelephonyManager telephonyManager = (TelephonyManager) ConfigManager  
    394                             .getContext().getSystemService(  
    395                                     Context.TELEPHONY_SERVICE);  
    396                     networkType = telephonyManager.getNetworkType();  
    397                 }  
    398             } else {  
    399                 networkType = NETWORK_TYPE_UNAVAILABLE;  
    400             }  
    401    
    402         } catch (Exception ex) {  
    403             ex.printStackTrace();  
    404         }  
    405         return getNetworkClassByType(networkType);  
    406    
    407     }  
    408    
    409     public static String getWifiRssi() {  
    410         int asu = 85;  
    411         try {  
    412             final NetworkInfo network = ((ConnectivityManager) ConfigManager  
    413                     .getContext()  
    414                     .getSystemService(Context.CONNECTIVITY_SERVICE))  
    415                     .getActiveNetworkInfo();  
    416             if (network != null && network.isAvailable()  
    417                     && network.isConnected()) {  
    418                 int type = network.getType();  
    419                 if (type == ConnectivityManager.TYPE_WIFI) {  
    420                     WifiManager wifiManager = (WifiManager) ConfigManager  
    421                             .getContext()  
    422                             .getSystemService(Context.WIFI_SERVICE);  
    423    
    424                     WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
    425                     if (wifiInfo != null) {  
    426                         asu = wifiInfo.getRssi();  
    427                     }  
    428                 }  
    429             }  
    430         } catch (Exception e) {  
    431             e.printStackTrace();  
    432         }  
    433         return asu + "dBm";  
    434     }  
    435    
    436     public static String getWifiSsid() {  
    437         String ssid = "";  
    438         try {  
    439             final NetworkInfo network = ((ConnectivityManager) ConfigManager  
    440                     .getContext()  
    441                     .getSystemService(Context.CONNECTIVITY_SERVICE))  
    442                     .getActiveNetworkInfo();  
    443             if (network != null && network.isAvailable()  
    444                     && network.isConnected()) {  
    445                 int type = network.getType();  
    446                 if (type == ConnectivityManager.TYPE_WIFI) {  
    447                     WifiManager wifiManager = (WifiManager) ConfigManager  
    448                             .getContext()  
    449                             .getSystemService(Context.WIFI_SERVICE);  
    450    
    451                     WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
    452                     if (wifiInfo != null) {  
    453                         ssid = wifiInfo.getSSID();  
    454                         if (ssid == null) {  
    455                             ssid = "";  
    456                         }  
    457                         ssid = ssid.replaceAll(""", "");  
    458                     }  
    459                 }  
    460             }  
    461         } catch (Exception e) {  
    462             e.printStackTrace();  
    463         }  
    464         return ssid;  
    465     }  
    466    
    467     /** 
    468      * 检查sim卡状态 
    469      *  
    470      * @param ctx 
    471      * @return 
    472      */ 
    473     public static boolean checkSimState() {  
    474         TelephonyManager tm = (TelephonyManager) ConfigManager.getContext()  
    475                 .getSystemService(Context.TELEPHONY_SERVICE);  
    476         if (tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT  
    477                 || tm.getSimState() == TelephonyManager.SIM_STATE_UNKNOWN) {  
    478             return false;  
    479         }  
    480    
    481         return true;  
    482     }  
    483    
    484     /** 
    485      * 获取imei 
    486      */ 
    487     public static String getImei() {  
    488         TelephonyManager mTelephonyMgr = (TelephonyManager) ConfigManager  
    489                 .getContext().getSystemService(Context.TELEPHONY_SERVICE);  
    490         String imei = mTelephonyMgr.getDeviceId();  
    491         if (imei == null) {  
    492             imei = "000000000000000";  
    493         }  
    494         return imei;  
    495     }  
    496    
    497     public static String getPhoneImsi() {  
    498         TelephonyManager mTelephonyMgr = (TelephonyManager) ConfigManager  
    499                 .getContext().getSystemService(Context.TELEPHONY_SERVICE);  
    500         return mTelephonyMgr.getSubscriberId();  
    501     }  
    502    
    503     public static CellInfo getNetInfo() {  
    504         CellInfo info = new CellInfo();  
    505         try {  
    506             TelephonyManager mTelephonyManager = (TelephonyManager) ConfigManager  
    507                     .getContext().getSystemService(Context.TELEPHONY_SERVICE);  
    508             String operator = mTelephonyManager.getNetworkOperator();  
    509             if (operator != null) {  
    510                 /** 通过operator获取 MCC 和MNC */ 
    511                 if (operator.length() > 3) {  
    512                     String mcc = operator.substring(0, 3);  
    513                     String mnc = operator.substring(3);  
    514                     info.setMcc(mcc);  
    515                     info.setMnc(mnc);  
    516                 }  
    517             }  
    518    
    519             int lac = 0;  
    520             int cellId = 0;  
    521             int phoneType = mTelephonyManager.getPhoneType();  
    522             if (phoneType == TelephonyManager.PHONE_TYPE_GSM) {  
    523                 GsmCellLocation location = (GsmCellLocation) mTelephonyManager  
    524                         .getCellLocation();  
    525                 /** 通过GsmCellLocation获取中国移动和联通 LAC 和cellID */ 
    526                 lac = location.getLac();  
    527                 cellId = location.getCid();  
    528             } else if (phoneType == TelephonyManager.PHONE_TYPE_CDMA) {  
    529                 CdmaCellLocation location = (CdmaCellLocation) mTelephonyManager  
    530                         .getCellLocation();  
    531                 lac = location.getNetworkId();  
    532                 cellId = location.getBaseStationId();  
    533                 cellId /= 16;  
    534             }  
    535             if (lac == 0 || cellId == 0) {  
    536                 List<NeighboringCellInfo> infos = mTelephonyManager  
    537                         .getNeighboringCellInfo();  
    538                 int lc = 0;  
    539                 int ci = 0;  
    540                 int rssi = 0;  
    541                 for (NeighboringCellInfo cell : infos) {  
    542                     // 根据邻区总数进行循环  
    543                     if (lc == 0 || ci == 0) {  
    544                         lc = cell.getLac();  
    545                         ci = cell.getCid();  
    546                         rssi = cell.getRssi();  
    547                     }  
    548                     // sb.append(" LAC : " + info.getLac());  
    549                     // // 取出当前邻区的LAC  
    550                     // sb.append(" CID : " + info.getCid());  
    551                     // // 取出当前邻区的CID  
    552                     // sb.append(" BSSS : " + (-113 + 2 * info.getRssi()) +  
    553                     // "
    "); // 获取邻区基站信号强度  
    554                 }  
    555                 rssi = -113 + 2 * rssi;  
    556             }  
    557         } catch (Exception e) {  
    558             e.printStackTrace();  
    559         }  
    560         return info;  
    561     }  
    562    
    563 }
  • 相关阅读:
    OpenStack网卡桥接问题
    Linux DD添加swap分区
    OpenStack KVM嵌套虚拟化的配置
    OpenStack KVM嵌套虚拟化的配置
    received packet with own address as source address
    mysql ERROR 1040 (08004): Too many connections
    openstack以其他tenant用户创建实例
    OpenStack Controller HA (3)
    OpenStack Controller HA (2)
    OpenStack controller HA
  • 原文地址:https://www.cnblogs.com/lr393993507/p/5542673.html
Copyright © 2020-2023  润新知