android 功能模块之通讯模块五

符号 阅读:625 2021-04-01 00:24:31 评论:0

Android通讯录开发之获取运营商号码段(移动、联通、电信)


2014年1月8日 开发记录
碎碎念:2014年的第一篇博客,原本是想写一篇随笔来开头,只因自己太懒把这件事忘记了,或者根本就不想写。我当实习生也当了接近半年了,工作上的内容和学到的东西有很多可以分享的,只有当自己工作遇到问题解决不了的时候才真正体会到有些地方自己没学扎实,我边反省边实习,自己也是从刚开始的小打小闹的开发经历到独立负责项目的开发,这是很大的一个转变,尽管如此,自己也有了前所未有的压力,自己没有想象的那么优秀,成为优秀是一个艰苦的过程,需要不断去领悟和提升。

碎碎念不想说太多,本篇博客是自己在开发时用到的一个点,获取运营商的号码段,截至2013年12月30日,三大运营商的号码段增加了不少,还出现了1700这样4位的号码段。号码段有什么用呢?每个运营商都有不同的号码段,比如159是移动的,185是联通的,189是电信的,通过号码段我们可以分辨出该号码是属于那个运营商的。

小巫开发是通过把三大运营商的号码段写到配置文件里,通过解析配置文件把号码段读出来,只好再进行比对。下面是实现:

写一个配置文件
/mobilemeeting/res/raw/config.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <config>  
  3.   
  4.     <!-- 移动现有号码段 -->  
  5.     <TEL_MOBILE>  
  6. 134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188  
  7.     </TEL_MOBILE>  
  8.   
  9.     <!-- 联通现有号码段 -->  
  10.     <TEL_UNICOM>  
  11. 130,131,132,155,156,185,186,145,176  
  12.     </TEL_UNICOM>  
  13.     <!-- 电信现有号码段 -->  
  14.     <TELECOM>  
  15. 133,153,180,181,189,1700,177  
  16.     </TELECOM>  
  17.   
  18. </config>  
<?xml version="1.0" encoding="utf-8"?> 
<config> 
 
    <!-- 移动现有号码段 --> 
    <TEL_MOBILE> 
134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188 
    </TEL_MOBILE> 
 
    <!-- 联通现有号码段 --> 
    <TEL_UNICOM> 
130,131,132,155,156,185,186,145,176 
    </TEL_UNICOM> 
    <!-- 电信现有号码段 --> 
    <TELECOM> 
133,153,180,181,189,1700,177 
    </TELECOM> 
 
</config>


写一个配置文件控制器
  1. package com.suntek.mobilemeeting.config;  
  2.   
  3. import java.io.InputStream;  
  4. import java.util.HashMap;  
  5.   
  6. import javax.xml.parsers.SAXParser;  
  7. import javax.xml.parsers.SAXParserFactory;  
  8.   
  9. import org.xml.sax.Attributes;  
  10. import org.xml.sax.SAXException;  
  11. import org.xml.sax.helpers.DefaultHandler;  
  12.   
  13. import android.text.TextUtils;  
  14.   
  15. /** 
  16.  * 配置文件控制器 用于获取配置文件 
  17.  *  
  18.  * @author wwj 
  19.  *  
  20.  */  
  21. public class ConfigController {  
  22.     private static ConfigController instance;  
  23.   
  24.     public static ConfigController getInstance() {  
  25.         if (instance == null) {  
  26.             synchronized (ConfigController.class) {  
  27.                 if (instance == null) {  
  28.                     instance = new ConfigController();  
  29.                 }  
  30.             }  
  31.         }  
  32.         return instance;  
  33.     }  
  34.   
  35.     public ConfigController() {  
  36.         InputStream input = getClass().getResourceAsStream(  
  37.                 "/res/raw/config.xml");  
  38.         try {  
  39.             config = new ConfigParser(input).getResult();  
  40.         } catch (Exception e) {  
  41.             config = new HashMap<String, String>();  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45.   
  46.     private HashMap<String, String> config;  
  47.   
  48.     public String get(String key) {  
  49.         return config.get(key);  
  50.     }  
  51.   
  52.     /** 
  53.      * 获取/res/raw/config.xml中的配置 
  54.      *  
  55.      * @param key 
  56.      *            配置名 
  57.      * @param failedValue 
  58.      *            获取配置失败时的取值:没有配置,或者配置不为boolean型 
  59.      */  
  60.     public boolean get(String key, boolean failedValue) {  
  61.         String stringValue = config.get(key);  
  62.         if (TextUtils.isEmpty(stringValue)  
  63.                 || !("true".equalsIgnoreCase(stringValue) || "false"  
  64.                         .equalsIgnoreCase(stringValue))) {  
  65.             return failedValue;  
  66.         } else {  
  67.             return Boolean.parseBoolean(stringValue);  
  68.         }  
  69.     }  
  70.   
  71.     class ConfigParser extends DefaultHandler {  
  72.         private StringBuffer accumulator;  
  73.         private HashMap<String, String> result;  
  74.   
  75.         public ConfigParser(InputStream input) throws Exception {  
  76.             SAXParserFactory factory = SAXParserFactory.newInstance();  
  77.             SAXParser parser = factory.newSAXParser();  
  78.             parser.parse(input, this);  
  79.         }  
  80.   
  81.         public void characters(char buffer[], int start, int length) {  
  82.             accumulator.append(buffer, start, length);  
  83.         }  
  84.   
  85.         public void endDocument() throws SAXException {  
  86.             super.endDocument();  
  87.         }  
  88.   
  89.         public void endElement(String uri, String localName, String qName)  
  90.                 throws SAXException {  
  91.             super.endElement(uri, localName, qName);  
  92.   
  93.             if (!"config".equals(localName)) { // "config" 是根元素  
  94.                 String key = localName;  
  95.                 String value = accumulator.toString();  
  96.                 result.put(key, value);  
  97.             }  
  98.         }  
  99.   
  100.         public void startDocument() throws SAXException {  
  101.             super.startDocument();  
  102.             accumulator = new StringBuffer();  
  103.             result = new HashMap<String, String>();  
  104.         }  
  105.   
  106.         public void startElement(String uri, String localName, String qName,  
  107.                 Attributes attributes) throws SAXException {  
  108.             accumulator.setLength(0);  
  109.         }  
  110.   
  111.         public HashMap<String, String> getResult() {  
  112.             return result;  
  113.         }  
  114.     }  
  115. }  
package com.suntek.mobilemeeting.config; 
 
import java.io.InputStream; 
import java.util.HashMap; 
 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 
 
import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 
 
import android.text.TextUtils; 
 
/** 
 * 配置文件控制器 用于获取配置文件 
 *  
 * @author wwj 
 *  
 */ 
public class ConfigController { 
	private static ConfigController instance; 
 
	public static ConfigController getInstance() { 
		if (instance == null) { 
			synchronized (ConfigController.class) { 
				if (instance == null) { 
					instance = new ConfigController(); 
				} 
			} 
		} 
		return instance; 
	} 
 
	public ConfigController() { 
		InputStream input = getClass().getResourceAsStream( 
				"/res/raw/config.xml"); 
		try { 
			config = new ConfigParser(input).getResult(); 
		} catch (Exception e) { 
			config = new HashMap<String, String>(); 
			e.printStackTrace(); 
		} 
	} 
 
	private HashMap<String, String> config; 
 
	public String get(String key) { 
		return config.get(key); 
	} 
 
	/** 
	 * 获取/res/raw/config.xml中的配置 
	 *  
	 * @param key 
	 *            配置名 
	 * @param failedValue 
	 *            获取配置失败时的取值:没有配置,或者配置不为boolean型 
	 */ 
	public boolean get(String key, boolean failedValue) { 
		String stringValue = config.get(key); 
		if (TextUtils.isEmpty(stringValue) 
				|| !("true".equalsIgnoreCase(stringValue) || "false" 
						.equalsIgnoreCase(stringValue))) { 
			return failedValue; 
		} else { 
			return Boolean.parseBoolean(stringValue); 
		} 
	} 
 
	class ConfigParser extends DefaultHandler { 
		private StringBuffer accumulator; 
		private HashMap<String, String> result; 
 
		public ConfigParser(InputStream input) throws Exception { 
			SAXParserFactory factory = SAXParserFactory.newInstance(); 
			SAXParser parser = factory.newSAXParser(); 
			parser.parse(input, this); 
		} 
 
		public void characters(char buffer[], int start, int length) { 
			accumulator.append(buffer, start, length); 
		} 
 
		public void endDocument() throws SAXException { 
			super.endDocument(); 
		} 
 
		public void endElement(String uri, String localName, String qName) 
				throws SAXException { 
			super.endElement(uri, localName, qName); 
 
			if (!"config".equals(localName)) { // "config" 是根元素 
				String key = localName; 
				String value = accumulator.toString(); 
				result.put(key, value); 
			} 
		} 
 
		public void startDocument() throws SAXException { 
			super.startDocument(); 
			accumulator = new StringBuffer(); 
			result = new HashMap<String, String>(); 
		} 
 
		public void startElement(String uri, String localName, String qName, 
				Attributes attributes) throws SAXException { 
			accumulator.setLength(0); 
		} 
 
		public HashMap<String, String> getResult() { 
			return result; 
		} 
	} 
}


写一个常量类
  1. package com.suntek.mobilemeeting.interfaces;  
  2.   
  3. import com.suntek.mobilemeeting.config.ConfigController;  
  4.   
  5. /** 
  6.  * 常量类 
  7.  *  
  8.  * @author wwj 
  9.  *  
  10.  */  
  11. public interface Const {  
  12.     String TEL_MOBILE = ConfigController.getInstance().get("TEL_MOBILE"); // 移动的号码段  
  13.     String TEL_UNICOM = ConfigController.getInstance().get("TEL_UNICOM"); // 联通的号码段  
  14.     String TELECOM = ConfigController.getInstance().get("TELECOM"); // 电信的号码段  
  15. }  
package com.suntek.mobilemeeting.interfaces; 
 
import com.suntek.mobilemeeting.config.ConfigController; 
 
/** 
 * 常量类 
 *  
 * @author wwj 
 *  
 */ 
public interface Const { 
	String TEL_MOBILE = ConfigController.getInstance().get("TEL_MOBILE"); // 移动的号码段 
	String TEL_UNICOM = ConfigController.getInstance().get("TEL_UNICOM"); // 联通的号码段 
	String TELECOM = ConfigController.getInstance().get("TELECOM"); // 电信的号码段 
} 



判断运营商的方法
  1. /** 
  2.      * <pre> 
  3.      *  Const为常量类或接口 
  4.      *  String TEL_MOBILE = config.get("TEL_MOBILE", "134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188");  //移动的号码段 
  5.      *  String TEL_UNICOM = config.get("TEL_UNICOM", "130,131,132,155,156,185,186,145,176");  //联通的号码段 
  6.      *  
  7.      * 截至2013年12月30日 三大运营商号码段 
  8.      * 移动:134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188 
  9.      * 联通:130,131,132,155,156,185,186,145,176 
  10.      * 电信:133,153,180,181,189,1700,177 
  11.      * </pre> 
  12.      *  
  13.      * @param tel 
  14.      * @return 运营商 (1=移动、2=联通) 
  15.      */  
  16.     public static byte getTelCompany(String tel) {  
  17.         String telHead = "";  
  18.         if (tel.substring(04).equals("1700")) {  
  19.             telHead = tel.substring(04);  
  20.         } else {  
  21.             telHead = tel.substring(03);  
  22.         }  
  23.   
  24.         if (isMobileUnicom(telHead, 1)) {  
  25.             return 1;  
  26.         }  
  27.         if (isMobileUnicom(telHead, 2)) {  
  28.             return 2;  
  29.         }  
  30.         if (isMobileUnicom(telHead, 3)) {  
  31.             return 3;  
  32.         }  
  33.   
  34.         return -1;  
  35.     }  
  36.   
  37.     /** 
  38.      * 判断是哪种类型号码段 
  39.      *  
  40.      * @param telHead 
  41.      * @param company 
  42.      * @return 
  43.      */  
  44.     private static boolean isMobileUnicom(String telHead, int company) {  
  45.         String tel = "";  
  46.   
  47.         switch (company) {  
  48.         case 1// 移动号码段  
  49.             tel = Const.TEL_MOBILE;  
  50.             break;  
  51.         case 2// 联通号码段  
  52.             tel = Const.TEL_UNICOM;  
  53.             break;  
  54.         case 3// 电信号码段  
  55.             tel = Const.TELECOM;  
  56.             break;  
  57.         default:  
  58.             return false;  
  59.         }  
  60.   
  61.         // 分割  
  62.         String[] aTel = tel.split(",");  
  63.   
  64.         int iCount = aTel.length;  
  65.   
  66.         for (int i = 0; i < iCount; i++) {  
  67.             if (aTel[i].equals(telHead)) {  
  68.                 return true;  
  69.             }  
  70.         }  
  71.   
  72.         return false;  
  73.     }  
/** 
	 * <pre> 
	 * 	Const为常量类或接口 
	 * 	String TEL_MOBILE = config.get("TEL_MOBILE", "134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188");  //移动的号码段 
	 * 	String TEL_UNICOM = config.get("TEL_UNICOM", "130,131,132,155,156,185,186,145,176");  //联通的号码段 
	 *  
	 * 截至2013年12月30日 三大运营商号码段 
	 * 移动:134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188 
	 * 联通:130,131,132,155,156,185,186,145,176 
	 * 电信:133,153,180,181,189,1700,177 
	 * </pre> 
	 *  
	 * @param tel 
	 * @return 运营商 (1=移动、2=联通) 
	 */ 
	public static byte getTelCompany(String tel) { 
		String telHead = ""; 
		if (tel.substring(0, 4).equals("1700")) { 
			telHead = tel.substring(0, 4); 
		} else { 
			telHead = tel.substring(0, 3); 
		} 
 
		if (isMobileUnicom(telHead, 1)) { 
			return 1; 
		} 
		if (isMobileUnicom(telHead, 2)) { 
			return 2; 
		} 
		if (isMobileUnicom(telHead, 3)) { 
			return 3; 
		} 
 
		return -1; 
	} 
 
	/** 
	 * 判断是哪种类型号码段 
	 *  
	 * @param telHead 
	 * @param company 
	 * @return 
	 */ 
	private static boolean isMobileUnicom(String telHead, int company) { 
		String tel = ""; 
 
		switch (company) { 
		case 1: // 移动号码段 
			tel = Const.TEL_MOBILE; 
			break; 
		case 2: // 联通号码段 
			tel = Const.TEL_UNICOM; 
			break; 
		case 3: // 电信号码段 
			tel = Const.TELECOM; 
			break; 
		default: 
			return false; 
		} 
 
		// 分割 
		String[] aTel = tel.split(","); 
 
		int iCount = aTel.length; 
 
		for (int i = 0; i < iCount; i++) { 
			if (aTel[i].equals(telHead)) { 
				return true; 
			} 
		} 
 
		return false; 
	} 


有需要的Android猿猿们拿去用吧。


标签:Android
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号