您的位置:首页 > 移动开发 > Android开发

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

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

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

写一个配置文件
/mobilemeeting/res/raw/config.xml

[html] view
plaincopy





<?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>

写一个配置文件控制器

[java] view
plaincopy





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;

}

}

}

写一个常量类

[java] view
plaincopy





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"); // 电信的号码段

}

判断运营商的方法

[java] view
plaincopy





/**

* <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猿猿们拿去用吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐