Android libphonenumber Demo 手机号码归属地

libphonenumber 是google 开源的库,提供手机号码格式化,来电归属地,运营商等多种功能十分强大,现在做个简单的demo

1、首先下载 libphonumber 相关的库 here,  下载 carrier-1.9.jar、geocoder-2.32.jar、libphonenumber-7.2.2.jar、/prefixmapper-2.32.jar 这4个库。放在源码目录 lib 文件夹下(本人用的是 AS), 右键--> add as library 。

2、接下来就是怎样使用 其中的api了,下面是geo 工具类

  1. public class GeoUtil {

  2. private static PhoneNumberUtil mPhoneNumberUtil = PhoneNumberUtil.getInstance();

  3. private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();

  4.     // 获取国家码 “CN”

  5. public static String getCurrentCountryIso(Context context) {

  6. // The {@link CountryDetector} should never return null so this is safe to return as-is.

  7. return CountryDetector.getInstance(context).getCurrentCountryIso();

  8. }

  9.     //获取归属地信息

  10. public static String getGeocodedLocationFor(Context context,String phoneNumber) {

  11. final PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();

  12. Phonenumber.PhoneNumber structuredNumber = getStructedNumber(context,phoneNumber);

  13. Locale locale = context.getResources().getConfiguration().locale;

  14. return geocoder.getDescriptionForNumber(structuredNumber,locale);

  15. }

  16.    //检查是否为 有效号码

  17. public static boolean checkPhoneNumber(Context context,String phoneNumber) {

  18. return mPhoneNumberUtil.isValidNumber(getStructedNumber(context,phoneNumber));

  19. }

  20. public static Phonenumber.PhoneNumber getStructedNumber(Context context,String phoneNumber) {

  21. try {

  22. final Phonenumber.PhoneNumber structuredNumber =

  23. mPhoneNumberUtil.parse(phoneNumber,getCurrentCountryIso(context));

  24. return structuredNumber;

  25. } catch (NumberParseException e) {

  26. return null;

  27. }

  28. }

  29.     //获取运营商信息

  30. public static String getCarrier(Context context,String phoneNumber) {

  31. Phonenumber.PhoneNumber structedNumber = getStructedNumber(context,phoneNumber);

  32. Locale locale = context.getResources().getConfiguration().locale;

  33. return carrierMapper.getNameForNumber(structedNumber,Locale.US);

  34. }

  35. }

以下为获取国家码的工具类

  1. public class CountryDetector {

  2. private static final String TAG = CountryDetector.class.getSimpleName();

  3. private static CountryDetector sInstance;

  4. private final Context mContext;

  5. private final TelephonyManager mTelephonyManager;

  6. private final LocationManager mLocationManager;

  7. private final LocaleProvider mLocaleProvider;

  8. private final String DEFAULT_COUNTRY_ISO = "CN";

  9. public static class LocaleProvider {

  10. public Locale getDefaultLocale() {

  11. return Locale.getDefault();

  12. }

  13. }

  14. private CountryDetector(Context context) {

  15. this(context,(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE),

  16. (LocationManager) context.getSystemService(Context.LOCATION_SERVICE),

  17. new LocaleProvider());

  18. }

  19. private CountryDetector(Context context, TelephonyManager telephonyManager,

  20. LocationManager locationManager, LocaleProvider localeProvider) {

  21. mTelephonyManager = telephonyManager;

  22. mLocationManager = locationManager;

  23. mLocaleProvider = localeProvider;

  24. mContext = context;

  25. }

  26. public static CountryDetector getInstance(Context context) {

  27. if(sInstance == null) {

  28. sInstance = new CountryDetector(context);

  29. }

  30. return sInstance;

  31. }

  32. public String getCurrentCountryIso() {

  33. String result = null;

  34. if (isNetworkCountryCodeAvailable()) {

  35. result = getNetworkBasedCountryIso();

  36. Log.d(TAG," getNetworkBasedCountryIso");

  37. }

  38. if (TextUtils.isEmpty(result)) {

  39. result = getSimBasedCountryIso();

  40. Log.d(TAG,"getSimBasedCountryIso");

  41. }

  42. if (TextUtils.isEmpty(result)) {

  43. result = getLocaleBasedCountryIso();

  44. Log.d(TAG,"getLocaleBasedCountryIso");

  45. }

  46. if (TextUtils.isEmpty(result)) {

  47. result = DEFAULT_COUNTRY_ISO;

  48. Log.d(TAG,"DEFAULT_COUNTRY_ISO");

  49. }

  50. Log.d(TAG," result == " + result);

  51. return result.toUpperCase(Locale.US);

  52. }

  53. /**

  54. * @return the country code of the current telephony network the user is connected to.

  55. */

  56. private String getNetworkBasedCountryIso() {

  57. return mTelephonyManager.getNetworkCountryIso();

  58. }

  59. /**

  60. * @return the country code of the SIM card currently inserted in the device.

  61. */

  62. private String getSimBasedCountryIso() {

  63. return mTelephonyManager.getSimCountryIso();

  64. }

  65. /**

  66. * @return the country code of the user's currently selected locale.

  67. */

  68. private String getLocaleBasedCountryIso() {

  69. Locale defaultLocale = mLocaleProvider.getDefaultLocale();

  70. if (defaultLocale != null) {

  71. return defaultLocale.getCountry();

  72. }

  73. return null;

  74. }

  75. private boolean isNetworkCountryCodeAvailable() {

  76. // On CDMA TelephonyManager.getNetworkCountryIso() just returns the SIM's country code.

  77. // In this case, we want to ignore the value returned and fallback to location instead.

  78. return mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM;

  79. }

  80. }

MainActivity

  1. protected void onCreate(Bundle savedInstanceState) {

  2. super.onCreate(savedInstanceState);

  3. setContentView(R.layout.activity_main);

  4. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

  5. setSupportActionBar(toolbar);

  6. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

  7. fab.setOnClickListener(new View.OnClickListener() {

  8. @Override

  9. public void onClick(View view) {

  10. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

  11. .setAction("Action", null).show();

  12. }

  13. });

  14. mEditNumber = (EditText) findViewById(R.id.number);

  15. geocoder = PhoneNumberOfflineGeocoder.getInstance();

  16. phoneNumberUtil = PhoneNumberUtil.getInstance();

  17. }

  18. public void submit(View v) {

  19. phoneNumber = mEditNumber.getText().toString().trim();

  20. boolean isValidnumber = GeoUtil.checkPhoneNumber(this,phoneNumber);

  21. String carrier = GeoUtil.getCarrier(this,phoneNumber);

  22. location = GeoUtil.getGeocodedLocationFor(this, phoneNumber);

  23. Intent intent = new Intent(this,SecondActivity.class);

  24. Bundle bundle = new Bundle();

  25. bundle.putBoolean("ifValidNumber",isValidnumber);

  26. bundle.putString("location", location);

  27. bundle.putString("carrier",carrier);

  28. intent.putExtras(bundle);

  29. startActivity(intent);

  30. }

4、

Github: https://github.com/googlei18n/libphonenumber

android source: http://androidxref.com/6.0.1_r10/xref/packages/apps/ContactsCommon/src/com/android/contacts/common/GeoUtil.java#34

网页版的demo:http://giggsey.com/libphonenumber/index.php

Android 系统(254)---Android libphonenumber Demo 手机号码归属地相关推荐

  1. Android libphonenumber Demo 手机号码归属地

    libphonenumber 是google 开源的库,提供手机号码格式化,来电归属地,运营商等多种功能十分强大,现在做个简单的demo 1.首先下载 libphonumber 相关的库 here,  ...

  2. Android系统架构-[Android取经之路]

    摘要:本节主要来讲解Android的系统架构 阅读本文大约需要花费10分钟. 文章首发微信公众号:IngresGe 专注于Android系统级源码分析,Android的平台设计,欢迎关注我,谢谢! 欢 ...

  3. android 服务端技术,移动应用服务器端开发(基于JSP技术)-2017 Android系统构架 Android系统构架.docx...

    Android系统构架 PAGE 1 目 录 TOC \o "1-3" \h \z \u 一.Android系统构架 1 二.Linux内核层 2 三.系统运行库层 3 (一)系统 ...

  4. 【android系统】android系统升级流程分析(二)---update升级包分析

    接下来我们将通过几篇文章来分析update.zip包在具体Android系统升级的过程,来理解Android系统中Recovery模式服务的工作原理.今天让我先来分析下升级包update.zip. 一 ...

  5. 【android系统】android系统升级流程分析(一)---recovery模式中进行update包升级流程分析

    今天我们直接来看下android中具体的升级过程是如何的. 升级流程概述 升级的流程图: 升级流程分析 第一步:升级包获取 升级获取可以通过远程下载,也可直接拷贝到指定目录即可. 第二步:准备升级 然 ...

  6. android log抓取方法,Android系统之Android抓取各种log的方法

    Android系统之Android抓取各种log的方法 2018年11月25日 | 萬仟网移动技术 | 我要评论 android之android抓取各种log的方法 1.logcat (四类log b ...

  7. Android系统(62)-----Android 7.1 新特性之 Shortcuts 介绍

    Android 7.1 新特性之 Shortcuts 介绍 Android 7.1 允许 App 自定义 Shortcuts,类似 iOS 的 3D touch.通过在桌面长按 App 弹出 Shor ...

  8. android系统语音合成,android 语音合成报错

    发现了2个问题 第一个貌似是复制离线的资源出错了(已经核对过读写等权限): 12-19 19:54:49.739 32006-32159/com.zhanglf.youxuanz I/NonBlock ...

  9. Android 系统(71)---Android系统build.prop文件生成过程

    Android系统build.prop文件生成过程 Android系统build.prop生成过程 这个文件类似于windows的注册表文件,定义了系统初始的一些参数属性,功能的开放等,通过调整或增加 ...

最新文章

  1. Python函数参数的五种类型
  2. 关于C#中的类访问修饰符
  3. windows下cipher和efsdump工具的初步使用
  4. COJ 1170 A Simple Problem
  5. MySQL必知必会(使用子查询)
  6. C#面向对象2 静态类、静态成员的理解
  7. python面值组合_算法题 - 拼凑面额 - Python
  8. 使用AirDrop将文件从iPhone或iPad传送到Mac电脑教程
  9. JFrame+JButton简单使用(菜鸟入门)——JAVA
  10. h2ouve下载 insyde_H20UVE_100.00.9.2 Insyde H2OUVE (UEFI Variable Editor) - 下载 - 搜珍网
  11. 人工智能历史回眸:达特茅斯会议
  12. 高次同余方程式的解数及解法
  13. 手机服务器 微信QQ,玩家天价买服务器语聊开黑 小白没想明白:微信QQ难道不行?...
  14. 信息时代,书香更宜人
  15. 布线光纤方面的知识都在这了,千万别错过!
  16. Cg学习记录003 之Varying参数
  17. Python matplot画柱状图(一)
  18. VSTO Office二次开发键盘鼠标钩子使用整理
  19. SpringBoot+Thymeleaf模板实现中英文页面文字翻译
  20. 【无标题】codesys与rte关系

热门文章

  1. (4)散列函数设计:除留余数法
  2. 从Nand Flash启动U-BOOT的基本原理
  3. [设计模式] - 工厂模式
  4. 嵌入式Linux系统编程学习之四Shell编程
  5. linux写文件操作同步,linux 可执行文件与写操作的同步问题(文件读写操作产生的锁机制)...
  6. 【LeetCode】剑指 Offer 13. 机器人的运动范围
  7. RPC与RMI的区别
  8. 不加载,手动实例化Service
  9. CentOS升级Python到2.7版本
  10. Fatal error: Call to undefined function: mysql_connect() 的解决