From:http://blog.youkuaiyun.com/peijiangping1989/article/details/17099625
我们想获取手机的运营商信息。通常都会去调用系统的TelephonyManager类的取数据。但是很多时候可能取不到卡的信息(例如双卡手机和一些特殊卡),这样就区别不了运营商了。但是有时候我们的需求要进行不通运营商的差异化定制。这样我们可以根据网络的判断运营商。
核心就是获取可用网络列表,比如你可用网络有cmwap cmnet这样你肯定就是移动的运营商了。当然这样的办法也有不行的时候,所以我们就2套一起来。把网络和获取设备卡的信息整个写成一个接口。至于双卡双待获取sim卡信息的问题。我马上会整理一篇博客给大家。主要是根据不同的方案商来写不同的接口。
核心代码如下
- private void getProviders() {
- NetWorkUtil nwu = new NetWorkUtil(this);
- String net = nwu.getNetWork();
- List<String> infos = nwu.getNetWorkList();
- if (net == null || net.equals("WIFI")) {
- if (infos.size() > 1) {
- infos.remove("WIFI");
- net = infos.get(0);
- if (net.equals("3gwap") || net.equals("uniwap")
- || net.equals("3gnet") || net.equals("uninet")) {
- Constants.MB_ID = 2;
- } else if (net.equals("cmnet") || net.equals("cmwap")) {
- Constants.MB_ID = 1;
- } else if (net.equals("ctnet") || net.equals("ctwap")) {
- Constants.MB_ID = 3;
- }
- } else {
- Constants.MB_ID = PhoneUtil.getProvidersName(this);
- }
- } else {
- if (net.equals("3gwap") || net.equals("uniwap")
- || net.equals("3gnet") || net.equals("uninet")) {
- Constants.MB_ID = 2;
- } else if (net.equals("cmnet") || net.equals("cmwap")) {
- Constants.MB_ID = 1;
- } else if (net.equals("ctnet") || net.equals("ctwap")) {
- Constants.MB_ID = 3;
- }
- }
- }
1是移动,2是联通,3是电信
- **
- * 作者: peijiangping<BR>
- * 时间:2012-12-21下午6:22:38<BR>
- * 功能:获取可用网络列表<BR>
- * 返回值:void<BR>
- */
- public List<String> getNetWorkList() {
- ConnectivityManager cm = (ConnectivityManager) c
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo[] infos = cm.getAllNetworkInfo();
- List<String> list = new ArrayList<String>();
- if (infos != null) {
- for (int i = 0; i < infos.length; i++) {
- NetworkInfo info = infos[i];
- String name = null;
- if (info.getTypeName().equals("WIFI")) {
- name = info.getTypeName();
- } else {
- name = info.getExtraInfo();
- }
- if (name != null && list.contains(name) == false) {
- list.add(name);
- // System.out.println(name);
- }
- }
- }
- return list;
- }
- public String getNetWork() {
- String NOWNET = null;
- ConnectivityManager cm = (ConnectivityManager) c
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = cm.getActiveNetworkInfo();
- if (info != null && info.isAvailable()) {
- if (info.getTypeName().equals("WIFI")) {
- NOWNET = info.getTypeName();
- } else {
- NOWNET = info.getExtraInfo();// cmwap/cmnet/wifi/uniwap/uninet
- }
- }
- return NOWNET;
- }
获取网络状态的信息
- /**
- * 作者: peijiangping<BR>
- * 时间:2012-12-17下午2:55:31<BR>
- * 功能:获取运营商信息<BR>
- * 返回值:int<BR>
- */
- public static int getProvidersName(Context c) {
- int ProvidersName = 0;
- try {
- TelephonyManager telephonyManager = (TelephonyManager) c
- .getSystemService(Context.TELEPHONY_SERVICE);
- String operator = telephonyManager.getSimOperator();
- if (operator == null || operator.equals("")) {
- operator = telephonyManager.getSubscriberId();
- }
- if (operator == null || operator.equals("")) {
- ToastUtil tu = new ToastUtil(c);
- tu.showDefultToast("未检测到sim卡信息!");
- }
- if (operator != null) {
- if (operator.startsWith("46000")
- || operator.startsWith("46002")) {
- ProvidersName = 1;
- } else if (operator.startsWith("46001")) {
- ProvidersName = 2;
- } else if (operator.startsWith("46003")) {
- ProvidersName = 3;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return ProvidersName;
- }
获取SIM卡信息(目前单卡可行)
嗯,再写一个根据电话号码解析出运营商
- public static int validateMobile(String mobile) {
- if (mobile == null) {
- return 0;
- }
- mobile = getRealPhoneNum(mobile);
- if (mobile.trim().length() != 11) {
- return 0;
- }
- if (mobile.trim().substring(0, 3).equals("134")
- || mobile.trim().substring(0, 3).equals("135")
- || mobile.trim().substring(0, 3).equals("136")
- || mobile.trim().substring(0, 3).equals("137")
- || mobile.trim().substring(0, 3).equals("138")
- || mobile.trim().substring(0, 3).equals("139")
- || mobile.trim().substring(0, 3).equals("182")
- || mobile.trim().substring(0, 3).equals("150")
- || mobile.trim().substring(0, 3).equals("151")
- || mobile.trim().substring(0, 3).equals("152")
- || mobile.trim().substring(0, 3).equals("157")
- || mobile.trim().substring(0, 3).equals("158")
- || mobile.trim().substring(0, 3).equals("159")
- || mobile.trim().substring(0, 3).equals("187")
- || mobile.trim().substring(0, 3).equals("188")) {
- return 1;
- } else if (mobile.trim().substring(0, 3).equals("130")
- || mobile.trim().substring(0, 3).equals("131")
- || mobile.trim().substring(0, 3).equals("132")
- || mobile.trim().substring(0, 3).equals("156")
- || mobile.trim().substring(0, 3).equals("185")
- || mobile.trim().substring(0, 3).equals("186")) {
- return 2;
- } else if (mobile.trim().substring(0, 3).equals("133")
- || mobile.trim().substring(0, 3).equals("153")
- || mobile.trim().substring(0, 3).equals("180")
- || mobile.trim().substring(0, 3).equals("189")) {
- return 3;
- }
- return 0;
- }
比较笨的方法。