android之来电知了--手机号码归属地查询

原文:http://www.cnblogs.com/zxl-jay/archive/2011/09/30/2196555.html

 

 

package cn.yj3g.TelphonemangerService;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.regex.Pattern;

import cn.yj3g.entity.TableContanst;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 定义一个归属地服务的类,为以后的归属地查询等提供方法。
 */
public class AddressService {
    private Context context;

    public AddressService(Context context) {
        this.context = context;
    }

    /**
     * 判断当前databases目录下有无phoneAddres.db
     */
    public boolean dbIsexit() {
        return context.getDatabasePath(TableContanst.ADDRESS_DB_NAME).exists();
    }

    /**
     * 将phoneAddres.db拷贝到datebases目录下
     */
    public void copyDB() throws Exception {
        File databases = new File(context.getFilesDir().getParentFile(),
                "databases"); // 找到data目录下的databases文件夹
        if (!databases.exists()) {
            databases.mkdirs();
        }
        File dbFile = new File(databases, TableContanst.ADDRESS_DB_NAME);
        FileOutputStream fos = new FileOutputStream(dbFile);
        InputStream is = this.getClass().getClassLoader()
                .getResourceAsStream(TableContanst.ADDRESS_DB_NAME); // 得到phoneAddress.db
        byte[] bs = new byte[1024];
        int length = 0;
        while ((length = is.read(bs)) != -1) {
            fos.write(bs, 0, length);
        }
        fos.flush();
        is.close();
    }

    /**
     * 获得来电的归属地 手机的正则表达式为:^1[358]\d{9}$
     */
    public String getAddress(String number) {
        String address = null;
        File file = context.getDatabasePath(TableContanst.ADDRESS_DB_NAME);
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getAbsolutePath(),
                null, SQLiteDatabase.OPEN_READONLY);
        if (db.isOpen()) {
            if (Pattern.compile("^1[358]\\d{9}$").matcher(number).matches()) {// 手机来电匹配
                String subNumber7 = number.substring(0, 7);
                Cursor cursor = db
                        .rawQuery(
                                "select p.name as province,c.name as city from address as a "
                                        + "inner join city as c on a.cityid=c._id "
                                        + "inner join province as p on c.provinceid=p._id "
                                        + "where a.mobileprefix=?",
                                new String[] { subNumber7 });
                address = getAddressInfo(cursor);
            } else {// 座机电话匹配
                Cursor cursor = null;
                String subNumber4 = null;
                String subNumber3 = null;
                switch (number.length()) {
                case 12: // 4位区号+8位电话号码
                    subNumber4 = number.substring(0, 4);
                    cursor = db
                            .rawQuery(
                                    "select p.name as province,c.name as city from city c "
                                            + "inner join province p on c.provinceid=p._id "
                                            + "where c.areacode=? "
                                            + "order by c._id asc limit 1",
                                    new String[] { subNumber4 });
                    address = getAddressInfo(cursor);
                    break;
                case 11:// 4位区号+8位电话号码 或者 3位区号+8位电话号码
                    subNumber4 = number.substring(0, 4);
                    subNumber3 = number.substring(0, 3);
                    cursor = db
                            .rawQuery(
                                    "select p.name as province,c.name as city from city c "
                                            + "inner join province p on c.provinceid=p._id "
                                            + "where c.areacode in(?,?) "
                                            + "order by c._id asc limit 1",
                                    new String[] { subNumber4, subNumber3 });
                    address = getAddressInfo(cursor);
                    break;
                case 10:// 3位区号+7位电话号码
                    subNumber3 = number.substring(0, 3);
                    cursor = db
                            .rawQuery(
                                    "select p.name as province,c.name as city from city c "
                                            + "inner join province p on c.provinceid=p._id "
                                            + "where c.areacode=? "
                                            + "order by c._id asc limit 1",
                                    new String[] { subNumber3 });
                    address = getAddressInfo(cursor);
                    break;
                case 8:
                case 7:// 本地号码
                    address = "本地号码";
                    break;
                case 4:
                    if (number.startsWith("555")) {
                        address = "模拟器";
                    } else {
                        cursor = db
                                .rawQuery(
                                        "select p.name as province,c.name as city from city c "
                                                + "inner join province p on c.provinceid=p._id "
                                                + "where c.areacode=? "
                                                + "order by c._id asc limit 1",
                                        new String[] { number });
                        address = getAddressInfo(cursor);
                    }
                    break;
                case 3:
                    cursor = db
                            .rawQuery(
                                    "select p.name as province,c.name as city from city c "
                                            + "inner join province p on c.provinceid=p._id "
                                            + "where c.areacode=? "
                                            + "order by c._id asc limit 1",
                                    new String[] { number });
                    address = getAddressInfo(cursor);
                    break;
                default:
                    break;
                }
            }
            db.close();
        }
        return address;
    }

    /**
     * 根据查询所得游标得到来电的具体归属地并返回
     */
    private String getAddressInfo(Cursor cursor) {
        if (cursor.moveToFirst()) {
            String province = cursor.getString(0);
            String city = cursor.getString(1);
            if (province.equals(city)) {
                return province;
            } else
                return province + "省" + city + "市";
        }
        cursor.close();
        return "";
    }

    /**
     * 得到来电的类型
     */
    public String getType(String number) {
        if (Pattern.compile("^1[38][0126]\\d{8}$").matcher(number).matches()) {
            return "联通";
        } else if (Pattern.compile("^1[358][1456789]\\d{8}$").matcher(number)
                .matches()) {
            return "移动";
        } else if (Pattern.compile("^1[358][039]\\d{8}$").matcher(number)
                .matches()) {
            return "电信";
        } else if (number.startsWith("555")) {
            return "模拟器";
        } else if (getAddress(number).length() == 0) {
            return "未知";
        } else
            return "固话";
    }

    /**
     * 根据查询输入框输入的号码找到响应的归属地区号
     */
    public String getAreacode(String number) {
        String areacode = null;
        File file = context.getDatabasePath(TableContanst.ADDRESS_DB_NAME);
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getAbsolutePath(),
                null, SQLiteDatabase.OPEN_READONLY);
        if (db.isOpen()) {
            if (Pattern.compile("^1[358]\\d{9}$").matcher(number).matches()) {// 手机来电匹配
                String subNumber7 = number.substring(0, 7);
                Cursor cursor = db
                        .rawQuery(
                                "select c.areacode as areacode from city as c "
                                        + "inner join address a on a.cityid=c._id "
                                        + "where a.mobileprefix=? order by c._id asc limit 1",
                                new String[] { subNumber7 });
                areacode = getAreacodeInfo(cursor);
            } else {
                if (number.length() == 3) {
                    Cursor cursor = db
                            .rawQuery(
                                    "select c.name as name from city as c "
                                            + "where c.areacode =? order by c._id asc limit 1",
                                    new String[] { number });
                    areacode = getAreacodeInfo2(cursor, number);
                    
                } else if (number.length() >= 4) {
                    String subNumber4 = number.substring(0, 4);
                    Cursor cursor = db
                            .rawQuery(
                                    "select c.name as name from city as c "
                                            + "where c.areacode =? order by c._id asc limit 1",
                                    new String[] { subNumber4 });
                    areacode = getAreacodeInfo2(cursor, subNumber4);
                }
            }
        }
        db.close();
        return areacode;
    }

    /**
     * 根据查询所得游标得到查询手机号码的归属地
     */
    private String getAreacodeInfo(Cursor cursor) {
        if (cursor.moveToFirst()) {
            return cursor.getString(0);
        }
        cursor.close();
        return "";
    }

    /**
     * 根据查询所得游标得到查询固话号码的归属地
     */
    private String getAreacodeInfo2(Cursor cursor, String number) {
        if (cursor.moveToFirst()) {
            return number;
        } else
            cursor.close();
        return "";

    }
}

posted @ 2013-12-23 14:39  jason.android  阅读(331)  评论(0编辑  收藏  举报