0
点赞
收藏
分享

微信扫一扫

Android通讯录开发之获取运营商号码段(移动、联通,android选择题题库

小编 2022-01-23 阅读 42

133,153,180,181,189,1700,177

写一个配置文件控制器

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

}

判断运营商的方法

/**

  • 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”); //联通的号码段

}

判断运营商的方法

/**

  • 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”); //联通的号码段

举报

相关推荐

0 条评论