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
- <?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>
<?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>
写一个配置文件控制器
- 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;
- }
- }
- }
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;
}
}
}
写一个常量类
- 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"); // 电信的号码段
- }
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"); // 电信的号码段
}
判断运营商的方法
- /**
- * <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;
- }
/**
* <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猿猿们拿去用吧。
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。