android开发如何获取电话号码的归属地信息
来源:互联网 发布:淘宝飞鱼运动是正品么 编辑:程序博客网 时间:2024/06/06 01:41
由于本人在蓝牙项目的开发中,需要在当拨打或接听电话时弹出的通话界面中需要显示其该号码的归属地等信息,所以今天决定将这个关于如何获取手机号码归属地的使用方法总结下来,以便将来所需,利人也利己。其实这个功能也是相当的常见,例如手机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)从而得出所属城市
源码点击下载
- android开发如何获取电话号码的归属地信息
- [Android] Android下实现电话号码归属地的查询
- Android下实现电话号码归属地的查询
- java实现电话号码归属地的查询
- 电话号码归属地
- 电话号码归属地查询
- android 获取手机归属地
- android获取手机归属地
- android获取手机归属地
- Android 获取手机号码归属地
- java通过电话号码获取归属地,区号,邮编
- ip获取归属地位置信息
- Android 利用Soap协议查询电话号码归属地
- Android本地数据库查询电话号码归属地规则
- Android studio 电话号码归属地查询app简易版
- Android studio 电话号码归属地查询app简易版
- java 电话号码归属地查询
- 电话号码归属地批量查询方法的优缺点对比
- linux下mount命令实现文件跨服务器的挂载
- 20171207-学习日记
- 60. 搜索插入位置
- 列举一些MongoEngine的filter方法
- c语言经典算法——查找一个整数数组中第二大数
- android开发如何获取电话号码的归属地信息
- 关于阿里矢量图iconfont的应用
- 总结 2017-12-07
- canvas调用webgl绘制图形图像
- C++标准库学习笔记-5-(Container)
- C到CPP的注释转换
- CentOS Redis 多实例
- 设计模式之中介者模式(Mediator)
- Spring Aop