Android 判斷用戶2G/3G/4G移動數據網絡

Android 判斷用戶2G/3G/4G移動數據網絡


在作 Android App 的時候,爲了給用戶省流量,爲了避免激起用戶的憤怒,爲了更好的用戶體驗,是須要根據用戶當前網絡狀況來作一些調整的,也能夠在 App 的設置模塊裏,讓用戶本身選擇,在 2G / 3G / 4G 網絡條件下,是否容許請求一些流量比較大的數據。html

經過 Android 提供的 TelephonyManager 和 ConnectivityManager 均可以獲取到 NetworksInfo 對象,能夠經過 getType() 獲取類型,判斷是 wifi 仍是 mobile ,若是是 mobile ,能夠經過 NetworksInfo 對象的 getSubType() 和 getSubTypeName() 能夠獲取到對於的網絡類型和名字。java

網絡類型和名字定義在 TelephonyManager 類裏。android

/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
網絡

看到這個代碼和註釋,相信沒有這方面知識的人很難看懂,都啥玩意?這注釋跟沒註釋有啥區別?!就是讓人看着更加鬧心而已。因此說,註釋對閱讀代碼的人很重要。固然這些東西可能太專業了,寫這些代碼的人估計是想寫也不知道該怎麼了,得寫多大一坨啊?!我在最後會貼上一些我整理的資料,能夠供你們參考一下,不是很詳細,也不專業,就是大概有個印象。app

TelephonyManager 還提供了 getNetworkTypeName(int type) 的方法,這個方法能夠返回一個字符串,可是信息量不大。ide

那怎麼判斷是 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;
}
}
post

而後下面是這幾個常量的值。spa

/** 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;
.net

不知道爲啥要把這些東西給隱藏起來,然道是不靠譜?!仍是其餘的更好的方式?!不知道,先這樣吧,如今經過上面的手段,是能夠知道用戶用的是什麼網絡,固然也能夠區分出來用戶使用的是 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

實例:

[java] view plaincopy

  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) + "\n" + 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(03);  

  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.                     // "\n"); // 獲取鄰區基站信號強度  

  554.                 }  

  555.                 rssi = -113 + 2 * rssi;  

  556.             }  

  557.         } catch (Exception e) {  

  558.             e.printStackTrace();  

  559.         }  

  560.         return info;  

  561.     }  

  562.   

相關文章
相關標籤/搜索