android开发如何获取电话号码的归属地信息

由于本人在蓝牙项目的开发中,需要在当拨打或接听电话时弹出的通话界面中需要显示其该号码的归属地等信息,所以今天决定将这个关于如何获取手机号码归属地的使用方法总结下来,以便将来所需,利人也利己。其实这个功能也是相当的常见,例如手机3G拨号时弹出的通话界面就有。

先上图,再做逻辑分析:
这里写图片描述
这里写图片描述
这里写图片描述
思路分析,以后要拿来用的时候直接看这边就好:

第一步:先做数据准备工作,如下
这里写图片描述
先用WriteToSD.java将telinfo.db写入SD卡:
关键代码

public class WriteToSD {
    private Context context;
    String filePath = Environment.getExternalStorageDirectory() + "/bluetooth";

    public WriteToSD(Context context) {
        this.context = context;
        if (!isExist()) {
            write();
        }
    }

    private void write() {
        InputStream inputStream;
        try {
            inputStream = context.getResources().getAssets().open("telinfo.db", 3);
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(filePath + "/telinfo.db");
            byte[] buffer = new byte[512];
            int count = 0;
            while ((count = inputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, count);
            }
            fileOutputStream.flush();
            fileOutputStream.close();
            inputStream.close();
            System.out.println("success");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private boolean isExist() {
        File file = new File(filePath + "/telinfo.db");
        if (file.exists()) {
            return true;
        } else {
            return false;
        }
    }

再用PhoneCardInfoDBHelper.java拿到这个数据库文件进行访问;我们先看看这个数据库文件里面是什么东西,如下
这里写图片描述
首先这个数据库文件放了很多张以首字母‘A’开头和后面接3个数字的表,每张表里面存储的都是2个字段,一个ID和一个int类型的整数字而已,有什么作用就要去看看CityDefine.java这个类了

CityDefine.java关键代码,如下:

public PhoneCardInfo onLookupEvent(String strTelNumber) {
        if (!TextUtils.isEmpty(strTelNumber)) {
            PhoneCardInfo info = new PhoneCardInfo();
            if (!checkAllNumber(strTelNumber)) {
                return null;
            }

            if (strTelNumber.equals("13800138000")) {
                info.mCityResID = 0;
                info.mOperatorResID = R.string.phone_info_6;
                return info;
            } else if (strTelNumber.equals("1008611")) {
                info.mCityResID = 0;
                info.mOperatorResID = R.string.phone_info_7;
                return info;
            } else if (strTelNumber.equals("95105105")) {
                info.mCityResID = 0;
                info.mOperatorResID = R.string.phone_info_9;
                return info;
            }

            if (strTelNumber.charAt(0) == '1' && strTelNumber.length() >= 7) {

                int nCityID = 0;
                String strOperatorID = strTelNumber.substring(0, 3);
                System.out.println("strOperatorID - " + strOperatorID);
                String strIndex = strTelNumber.substring(3, 7);
                String strTableName = "A" + strOperatorID;

                info.mCityResID = 0;
                // 根据号码的前3位,判断是电信,联通,移动或者联通数据卡
                info.mOperatorResID = findOperatorInfo(Integer.valueOf(strOperatorID));
                System.out.println("mOperatorResID - " + info.mOperatorResID);

                if (info.mOperatorResID != 0) {
                    System.out.println("strIndex_substring(3, 7) - " + strIndex);
                    // 根据号码的前4-7位和数据表,判断改号码属于的当前城市ID号
                    nCityID = readTelInfoFromDB(strTableName, Integer.valueOf(strIndex));
                    System.out.println("nCityID - " + nCityID);

                    if (nCityID != 0) {
                        // 根据城市ID号获取该城市名称
                        info.mCityResID = findCityInfo(nCityID);
                        System.out.println("info.mCityResID  - " + info.mCityResID);
                    }
                    return info;
                }

            } else if (strTelNumber.charAt(0) == '1' && strTelNumber.length() == 5) {

                info.mCityResID = 0;
                info.mOperatorResID = findSpecialNumber(Integer.valueOf(strTelNumber));
                return info;
            } else if (strTelNumber.charAt(0) == '0' && strTelNumber.length() >= 3) {

                String strIndex = new String();
                if (strTelNumber.charAt(1) == '1' || strTelNumber.charAt(1) == '2') {
                    strIndex = strTelNumber.substring(1, 3);
                } else if (strTelNumber.length() >= 4) {
                    strIndex = strTelNumber.substring(1, 4);
                } else {
                    return null;
                }

                info.mCityResID = 0;
                info.mOperatorResID = findAreaInfo(Integer.valueOf(strIndex));
                return info;
            }
        }

        return null;
    }

由以上代码可见,数据库每张表的表名数字部分就是要判断的号码的前3位数字,这边是分情况而定,根据号码的第一位和长度或者作特别号码判断,其中特别号码为:比如13800,1008611等;那么其它的我们普通用户用的手机号码一般为11位这边都做了不同判断处理,根据号码的长度,然后根据号码的不同段位来获取该号码是属于电信,联通,和移动的,再来就是根据号码中间的几位来判断这个号码是属于哪个城市ID号进而在根据城市ID号得到该号码属于哪个城市,这个城市ID号和手机中的某个段位的号码相互匹配,就在数据库telinfo.db中各个表中,例如我这边要查18899858039这个号码,首先根据188可以查找到该号码是属于移动的,代码如下:

private void initOperatorInfoMap() {
        g_OperatorInfoMap = new SparseIntArray();
        g_OperatorInfoMap.put(130, R.string.card_info_1);
        g_OperatorInfoMap.put(131, R.string.card_info_1);
        g_OperatorInfoMap.put(132, R.string.card_info_1);
        g_OperatorInfoMap.put(133, R.string.card_info_3);
        g_OperatorInfoMap.put(134, R.string.card_info_2);
        g_OperatorInfoMap.put(135, R.string.card_info_2);
        g_OperatorInfoMap.put(136, R.string.card_info_2);
        g_OperatorInfoMap.put(137, R.string.card_info_2);
        g_OperatorInfoMap.put(138, R.string.card_info_2);
        g_OperatorInfoMap.put(139, R.string.card_info_2);

        g_OperatorInfoMap.put(145, R.string.card_info_4);
        g_OperatorInfoMap.put(147, R.string.card_info_2);

        g_OperatorInfoMap.put(150, R.string.card_info_2);
        g_OperatorInfoMap.put(151, R.string.card_info_2);
        g_OperatorInfoMap.put(152, R.string.card_info_2);
        g_OperatorInfoMap.put(153, R.string.card_info_3);
        g_OperatorInfoMap.put(155, R.string.card_info_1);
        g_OperatorInfoMap.put(156, R.string.card_info_1);
        g_OperatorInfoMap.put(157, R.string.card_info_2);
        g_OperatorInfoMap.put(158, R.string.card_info_2);
        g_OperatorInfoMap.put(159, R.string.card_info_2);

        g_OperatorInfoMap.put(180, R.string.card_info_3);
        g_OperatorInfoMap.put(181, R.string.card_info_3);
        g_OperatorInfoMap.put(182, R.string.card_info_2);
        g_OperatorInfoMap.put(183, R.string.card_info_2);
        g_OperatorInfoMap.put(184, R.string.card_info_2);
        g_OperatorInfoMap.put(185, R.string.card_info_1);
        g_OperatorInfoMap.put(186, R.string.card_info_1);
        g_OperatorInfoMap.put(187, R.string.card_info_2);
        g_OperatorInfoMap.put(188, R.string.card_info_2);
        g_OperatorInfoMap.put(189, R.string.card_info_3);

    }

这个数据在本地string.xml中已经作了匹配处理,然后根据188这三位即可去查数据表A188这张表所对应的ID号为’9986’的字段所对应的值(由于cursor在查询时候ID是从0开始数的所以这边要+1)从而得出所属城市

源码点击下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值