Android 根据网络分析运营商信息

来源:互联网 发布:淘宝客自动推广软件 编辑:程序博客网 时间:2024/04/30 15:15

From:http://blog.csdn.net/peijiangping1989/article/details/17099625


我们想获取手机的运营商信息。通常都会去调用系统的TelephonyManager类的取数据。但是很多时候可能取不到卡的信息(例如双卡手机和一些特殊卡),这样就区别不了运营商了。但是有时候我们的需求要进行不通运营商的差异化定制。这样我们可以根据网络的判断运营商。

核心就是获取可用网络列表,比如你可用网络有cmwap cmnet这样你肯定就是移动的运营商了。当然这样的办法也有不行的时候,所以我们就2套一起来。把网络和获取设备卡的信息整个写成一个接口。至于双卡双待获取sim卡信息的问题。我马上会整理一篇博客给大家。主要是根据不同的方案商来写不同的接口。

核心代码如下

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. private void getProviders() {  
  2.         NetWorkUtil nwu = new NetWorkUtil(this);  
  3.         String net = nwu.getNetWork();  
  4.         List<String> infos = nwu.getNetWorkList();  
  5.         if (net == null || net.equals("WIFI")) {  
  6.             if (infos.size() > 1) {  
  7.                 infos.remove("WIFI");  
  8.                 net = infos.get(0);  
  9.                 if (net.equals("3gwap") || net.equals("uniwap")  
  10.                         || net.equals("3gnet") || net.equals("uninet")) {  
  11.                     Constants.MB_ID = 2;  
  12.                 } else if (net.equals("cmnet") || net.equals("cmwap")) {  
  13.                     Constants.MB_ID = 1;  
  14.                 } else if (net.equals("ctnet") || net.equals("ctwap")) {  
  15.                     Constants.MB_ID = 3;  
  16.                 }  
  17.             } else {  
  18.                 Constants.MB_ID = PhoneUtil.getProvidersName(this);  
  19.             }  
  20.         } else {  
  21.             if (net.equals("3gwap") || net.equals("uniwap")  
  22.                     || net.equals("3gnet") || net.equals("uninet")) {  
  23.                 Constants.MB_ID = 2;  
  24.             } else if (net.equals("cmnet") || net.equals("cmwap")) {  
  25.                 Constants.MB_ID = 1;  
  26.             } else if (net.equals("ctnet") || net.equals("ctwap")) {  
  27.                 Constants.MB_ID = 3;  
  28.             }  
  29.         }  
  30.     }  

1是移动,2是联通,3是电信

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. **  
  2.      * 作者: peijiangping<BR>  
  3.      * 时间:2012-12-21下午6:22:38<BR>  
  4.      * 功能:获取可用网络列表<BR>  
  5.      * 返回值:void<BR>  
  6.      */  
  7.     public List<String> getNetWorkList() {  
  8.         ConnectivityManager cm = (ConnectivityManager) c  
  9.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  10.         NetworkInfo[] infos = cm.getAllNetworkInfo();  
  11.         List<String> list = new ArrayList<String>();  
  12.         if (infos != null) {  
  13.             for (int i = 0; i < infos.length; i++) {  
  14.                 NetworkInfo info = infos[i];  
  15.                 String name = null;  
  16.                 if (info.getTypeName().equals("WIFI")) {  
  17.                     name = info.getTypeName();  
  18.                 } else {  
  19.                     name = info.getExtraInfo();  
  20.                 }  
  21.                 if (name != null && list.contains(name) == false) {  
  22.                     list.add(name);  
  23.                     // System.out.println(name);  
  24.                 }  
  25.             }  
  26.         }  
  27.         return list;  
  28.     }  
  29.   
  30.     public String getNetWork() {  
  31.         String NOWNET = null;  
  32.         ConnectivityManager cm = (ConnectivityManager) c  
  33.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  34.         NetworkInfo info = cm.getActiveNetworkInfo();  
  35.         if (info != null && info.isAvailable()) {  
  36.             if (info.getTypeName().equals("WIFI")) {  
  37.                 NOWNET = info.getTypeName();  
  38.             } else {  
  39.                 NOWNET = info.getExtraInfo();// cmwap/cmnet/wifi/uniwap/uninet  
  40.             }  
  41.         }  
  42.         return NOWNET;  
  43.     }  

获取网络状态的信息

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 作者: peijiangping<BR> 
  3.      * 时间:2012-12-17下午2:55:31<BR> 
  4.      * 功能:获取运营商信息<BR> 
  5.      * 返回值:int<BR> 
  6.      */  
  7.     public static int getProvidersName(Context c) {  
  8.         int ProvidersName = 0;  
  9.         try {  
  10.             TelephonyManager telephonyManager = (TelephonyManager) c  
  11.                     .getSystemService(Context.TELEPHONY_SERVICE);  
  12.             String operator = telephonyManager.getSimOperator();  
  13.             if (operator == null || operator.equals("")) {  
  14.                 operator = telephonyManager.getSubscriberId();  
  15.             }  
  16.             if (operator == null || operator.equals("")) {  
  17.                 ToastUtil tu = new ToastUtil(c);  
  18.                 tu.showDefultToast("未检测到sim卡信息!");  
  19.             }  
  20.             if (operator != null) {  
  21.                 if (operator.startsWith("46000")  
  22.                         || operator.startsWith("46002")) {  
  23.                     ProvidersName = 1;  
  24.                 } else if (operator.startsWith("46001")) {  
  25.                     ProvidersName = 2;  
  26.                 } else if (operator.startsWith("46003")) {  
  27.                     ProvidersName = 3;  
  28.                 }  
  29.             }  
  30.         } catch (Exception e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.         return ProvidersName;  
  34.     }  

获取SIM卡信息(目前单卡可行)

嗯,再写一个根据电话号码解析出运营商

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public static int validateMobile(String mobile) {  
  2.         if (mobile == null) {  
  3.             return 0;  
  4.         }  
  5.         mobile = getRealPhoneNum(mobile);  
  6.         if (mobile.trim().length() != 11) {  
  7.             return 0;  
  8.         }  
  9.         if (mobile.trim().substring(03).equals("134")  
  10.                 || mobile.trim().substring(03).equals("135")  
  11.                 || mobile.trim().substring(03).equals("136")  
  12.                 || mobile.trim().substring(03).equals("137")  
  13.                 || mobile.trim().substring(03).equals("138")  
  14.                 || mobile.trim().substring(03).equals("139")  
  15.                 || mobile.trim().substring(03).equals("182")  
  16.                 || mobile.trim().substring(03).equals("150")  
  17.                 || mobile.trim().substring(03).equals("151")  
  18.                 || mobile.trim().substring(03).equals("152")  
  19.                 || mobile.trim().substring(03).equals("157")  
  20.                 || mobile.trim().substring(03).equals("158")  
  21.                 || mobile.trim().substring(03).equals("159")  
  22.                 || mobile.trim().substring(03).equals("187")  
  23.                 || mobile.trim().substring(03).equals("188")) {  
  24.             return 1;  
  25.         } else if (mobile.trim().substring(03).equals("130")  
  26.                 || mobile.trim().substring(03).equals("131")  
  27.                 || mobile.trim().substring(03).equals("132")  
  28.                 || mobile.trim().substring(03).equals("156")  
  29.                 || mobile.trim().substring(03).equals("185")  
  30.                 || mobile.trim().substring(03).equals("186")) {  
  31.             return 2;  
  32.         } else if (mobile.trim().substring(03).equals("133")  
  33.                 || mobile.trim().substring(03).equals("153")  
  34.                 || mobile.trim().substring(03).equals("180")  
  35.                 || mobile.trim().substring(03).equals("189")) {  
  36.             return 3;  
  37.         }  
  38.         return 0;  
  39.     }  

比较笨的方法。
0 0
原创粉丝点击