https://blog.csdn.net/u013122625/article/details/52886440

前言

有个需求:接听接话使,皮套界面要能解析号码的来源地,并且要和系统的语言保持一致。这个问题说难不难,但是也不简单,一般情况下,我们可能会想到建立一个数据库,然后写一个ContentPrivoder,然后在项目中使用提供的URI来解析电话号码。

如果你真的这么想,那么你这个任务就是无尽的任务了。因为你需要适配各种语言,需要收集这个国家的地名,这简直是不可能完成的任务!

解决办法:用Google的公共电话号码解析库—libphonenumber

这个库Android源码的路径是:

    external/libphonenumber/当然了,github上面也有,地址是:[libphonenumber](https://github.com/googlei18n/libphonenumber)
  • 1
  • 2
  • 3
  • 4

由于我本人是直接使用Android源码下的库,所以我接下来用它来举例子。

首先用使用这个库,有两个方法:

  • 将libphonenumber作为module放到你的工程里
  • 将libphonenumber作为jar放到Android工程的libs目录下

步骤

在这里我们使用第二种,至于第一种方法不会使用的,可以问度娘,接来我说说使用步骤:

  1. 编译libphonenumber 
    使用mmm external/libphonenumber/,之后在终端下看到这个: 

  2. 找到对应的jar包

    看图可以知道,out下生成了很多jar包,我们选倒数第二个。为了便于识别我们把classes.jar改名为libphonenumber.jar。

  3. 导入到工程里

    我用的是Android Studio,也希望也用这个工具,毕竟谷歌推荐用这款软件。 
    首先,复制到libs下: 

    接着配置app那级的build.gradle,在dependencies里面添加一行:

    compile files('libs/libphonenumber.jar')
  • 1

如图: 

当然了,如果你的dependencies有下面这么一句代码,那就不用在手动添加,它会自动包含libs目录下面所有的jar。

    compile fileTree(dir: 'libs', include: ['*.jar'])
  • 1

4.在代码里面使用该库的接口

可以仿照源码InCallUI模块下面CallerInfo.java类下面的getGeoDescription方法:

/*** @return a geographical description string for the specified number.* @see com.android.i18n.phonenumbers.PhoneNumberOfflineGeocoder*/private static String getGeoDescription(Context context, String number) {Log.v(TAG, "getGeoDescription('" + number + "')...");if (TextUtils.isEmpty(number)) {return null;}PhoneNumberUtil util = PhoneNumberUtil.getInstance();PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();Locale locale = context.getResources().getConfiguration().locale;String countryIso = TelephonyManagerUtils.getCurrentCountryIso(context, locale);PhoneNumber pn = null;try {Log.v(TAG, "parsing '" + number+ "' for countryIso '" + countryIso + "'...");pn = util.parse(number, countryIso);Log.v(TAG, "- parsed number: " + pn);} catch (NumberParseException e) {Log.v(TAG, "getGeoDescription: NumberParseException for incoming number '" +number + "'");}if (pn != null) {String description = geocoder.getDescriptionForNumber(pn, locale);Log.v(TAG, "- got description: '" + description + "'");return description;}return null;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

这是我自己为了调用该接口,创建的一个类:

import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;import java.util.Locale;/*** Created by neal on 9/6/16.*/
public class CallerInfo {private static final String TAG = "CallerInfo";/*** @return a geographical description string for the specified number.* @see com.android.i18n.phonenumbers.PhoneNumberOfflineGeocoder*/public static String getGeoDescription(Context context, String number) {android.util.Log.d(TAG, "getGeoDescription('" + number + "')...");if (TextUtils.isEmpty(number)) {return null;}PhoneNumberUtil util = PhoneNumberUtil.getInstance();PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();Locale locale = context.getResources().getConfiguration().locale;final TelephonyManager telephonyManager =(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);String countryIso = telephonyManager.getNetworkCountryIso().toUpperCase();if (countryIso == null) {countryIso = locale.getCountry();Log.w(TAG, "No CountryDetector; falling back to countryIso based on locale: "+ countryIso);}Phonenumber.PhoneNumber pn = null;try {android.util.Log.d(TAG, "parsing '" + number+ "' for countryIso '" + countryIso + "'...");pn = util.parse(number, countryIso);android.util.Log.d(TAG, "- parsed number: " + pn);} catch (NumberParseException e) {android.util.Log.d(TAG, "getGeoDescription: NumberParseException for incoming number '" +number + "'");}if (pn != null) {String description = geocoder.getDescriptionForNumber(pn, locale);android.util.Log.d(TAG, "- got description: '" + description + "'");return description;}return null;}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

最后你在需要获取地名的地方调用:

String address = CallerInfo.getGeoDescription(mContext,number);

libphonenumber:Google的公共电话号码解析库相关推荐

  1. Google公共电话号码解析库: libphonenumber

    libphonenumber是用于解析.格式化.存储和校验电话号码的Java.C++或JavaScript类库.2010年,libphonenumber的Java实现首次以开源形式发布.在2014年, ...

  2. gson解析天气json_几种常用JSON解析库性能比较

    PS:公众号推文时间工作日早晨8点50分,周末下午3点30分,不见不散哈! 作者:飞污熊 xncoding.com/2018/01/09/java/jsons.html 本篇通过JMH来测试一下Jav ...

  3. 常用的python命令行解析库

    常用的python命令行解析库,这儿介绍3种: 1.argparse 2.click 3.fire argparse是python自带的模块,要经历解析器初始化.参数定义.解析一套流程,使用起来有些繁 ...

  4. Python3 爬虫学习笔记 C07 【解析库 lxml】

    Python3 爬虫学习笔记第七章 -- [解析库 lxml] 文章目录 [7.1]关于 lxml [7.2]使用 XPath [7.3]查找所有节点 [7.4]查找子节点 [7.5]查找父节点 [7 ...

  5. IOS学习:常用第三方库(GDataXMLNode:xml解析库)

    IOS学习:常用第三方库(GDataXMLNode:xml解析库) 解析 XML 通常有两种方式,DOM 和 SAX: DOM解析XML时,读入整个XML文档并构建一个驻留内存的树结构(节点树),通过 ...

  6. 8.8.8.8,Google推出免费DNS解析服务器

    8.8.8.8,Google推出免费DNS解析服务器 Google今天又给了我们一个惊喜,并沉重的打击了OpenDNS:他们刚刚宣布向所有的互联网用户提供一组快速,安全并且完全免费的DNS解析服务器, ...

  7. 全球免费公共 DNS 解析服务器 IP 地址列表推荐 (解决无法上网/加速/防劫持)

    全球免费公共 DNS 解析服务器 IP 地址列表推荐 基本上接触过网络相关知识的人应该多少都会听过 DNS 这个名词.因为 DNS 它非常重要,在我们上网的过程中扮演着重要的角色--"将网址 ...

  8. Epub文件解析库-- Epublib

    介绍 epublib是一个比较强大的Epub 文件解析库,同时也支持Epub文件的生成,本篇文章会对其做一个详细的介绍. 相关资料 项目地址 https://github.com/psiegman/e ...

  9. 国外免费公共DNS解析服务器

    最新的 OpenerDNS: 42.120.21.30 Google DNS: 8.8.8.8 8.8.4.4 OpenDNS: 208.67.222.222 208.67.220.220 诺顿 DN ...

最新文章

  1. [译] JWT 与 Spring Cloud 微服务
  2. makefile跨平台,动态库,静态库
  3. Linux下快捷键使用
  4. docker 推送到本地仓库_Docker_学习笔记系列之仓库
  5. SAP物料移动类型和自动科目设置(包含财务,pp)
  6. xtrbackup更换数据库_XtraBackup 备份还原 MySQL 数据库
  7. 为什么年轻人挣得很多还是穷?北上广深挑战指数报告~
  8. 论文翻译:《Improved Neural Relation Detection for Knowledge Base Question Answering》
  9. 以后再别这样做,否则你的苹果华为将挂在墙上
  10. Altium Designer(一):SCH
  11. 使用pt-query-digest,找到不是很合适的sql
  12. Linux虚拟网络基础 — Tap
  13. Android软键盘删除键触发Activity的返回事件
  14. 联想笔记本键盘排线_笔记本键盘排线怎么拆 thinkpad
  15. phalapi-入门篇6(小技巧和浅谈API适用范围以及入门篇总结)
  16. Docker及常用软件的安装部署
  17. 完美解决浏览器主页被hao123劫持,打开浏览器时自动进入hao123主页怎么办
  18. PMBOK(第六版) PMP笔记——《十三》第十三章(项目干系人管理)
  19. BZOJ 1002 1003 1007 被屠记录
  20. 用安卓手机看小说,阅读器APP怎么选

热门文章

  1. 三菱fx5u和伺服定位,5u和3u定位用法不一样,这个包含触摸屏,plc程序,plc参数设置
  2. android压力测试命令monkey详解
  3. calibre中的hcell_Calibre LVS BOX 详细用法
  4. Android案例(1)——美女拼图小游戏
  5. 罚函数法外点matlab,内点罚函数法matlab
  6. JS的Math对象,求数组的最大值max(),最小值min(),随机数 random()
  7. Ghostery – 隐私广告拦截工具
  8. 花式实现时间轴,样式由你来定!
  9. 读《文明之光》第三册 吴军
  10. 什么是合成数据 (Synthetic Data)?