Android 系统(254)---Android libphonenumber Demo 手机号码归属地
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 工具类
public class GeoUtil {
private static PhoneNumberUtil mPhoneNumberUtil = PhoneNumberUtil.getInstance();
private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();
// 获取国家码 “CN”
public static String getCurrentCountryIso(Context context) {
// The {@link CountryDetector} should never return null so this is safe to return as-is.
return CountryDetector.getInstance(context).getCurrentCountryIso();
}
//获取归属地信息
public static String getGeocodedLocationFor(Context context,String phoneNumber) {
final PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
Phonenumber.PhoneNumber structuredNumber = getStructedNumber(context,phoneNumber);
Locale locale = context.getResources().getConfiguration().locale;
return geocoder.getDescriptionForNumber(structuredNumber,locale);
}
//检查是否为 有效号码
public static boolean checkPhoneNumber(Context context,String phoneNumber) {
return mPhoneNumberUtil.isValidNumber(getStructedNumber(context,phoneNumber));
}
public static Phonenumber.PhoneNumber getStructedNumber(Context context,String phoneNumber) {
try {
final Phonenumber.PhoneNumber structuredNumber =
mPhoneNumberUtil.parse(phoneNumber,getCurrentCountryIso(context));
return structuredNumber;
} catch (NumberParseException e) {
return null;
}
}
//获取运营商信息
public static String getCarrier(Context context,String phoneNumber) {
Phonenumber.PhoneNumber structedNumber = getStructedNumber(context,phoneNumber);
Locale locale = context.getResources().getConfiguration().locale;
return carrierMapper.getNameForNumber(structedNumber,Locale.US);
}
}
以下为获取国家码的工具类
public class CountryDetector {
private static final String TAG = CountryDetector.class.getSimpleName();
private static CountryDetector sInstance;
private final Context mContext;
private final TelephonyManager mTelephonyManager;
private final LocationManager mLocationManager;
private final LocaleProvider mLocaleProvider;
private final String DEFAULT_COUNTRY_ISO = "CN";
public static class LocaleProvider {
public Locale getDefaultLocale() {
return Locale.getDefault();
}
}
private CountryDetector(Context context) {
this(context,(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE),
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE),
new LocaleProvider());
}
private CountryDetector(Context context, TelephonyManager telephonyManager,
LocationManager locationManager, LocaleProvider localeProvider) {
mTelephonyManager = telephonyManager;
mLocationManager = locationManager;
mLocaleProvider = localeProvider;
mContext = context;
}
public static CountryDetector getInstance(Context context) {
if(sInstance == null) {
sInstance = new CountryDetector(context);
}
return sInstance;
}
public String getCurrentCountryIso() {
String result = null;
if (isNetworkCountryCodeAvailable()) {
result = getNetworkBasedCountryIso();
Log.d(TAG," getNetworkBasedCountryIso");
}
if (TextUtils.isEmpty(result)) {
result = getSimBasedCountryIso();
Log.d(TAG,"getSimBasedCountryIso");
}
if (TextUtils.isEmpty(result)) {
result = getLocaleBasedCountryIso();
Log.d(TAG,"getLocaleBasedCountryIso");
}
if (TextUtils.isEmpty(result)) {
result = DEFAULT_COUNTRY_ISO;
Log.d(TAG,"DEFAULT_COUNTRY_ISO");
}
Log.d(TAG," result == " + result);
return result.toUpperCase(Locale.US);
}
/**
* @return the country code of the current telephony network the user is connected to.
*/
private String getNetworkBasedCountryIso() {
return mTelephonyManager.getNetworkCountryIso();
}
/**
* @return the country code of the SIM card currently inserted in the device.
*/
private String getSimBasedCountryIso() {
return mTelephonyManager.getSimCountryIso();
}
/**
* @return the country code of the user's currently selected locale.
*/
private String getLocaleBasedCountryIso() {
Locale defaultLocale = mLocaleProvider.getDefaultLocale();
if (defaultLocale != null) {
return defaultLocale.getCountry();
}
return null;
}
private boolean isNetworkCountryCodeAvailable() {
// On CDMA TelephonyManager.getNetworkCountryIso() just returns the SIM's country code.
// In this case, we want to ignore the value returned and fallback to location instead.
return mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM;
}
}
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
mEditNumber = (EditText) findViewById(R.id.number);
geocoder = PhoneNumberOfflineGeocoder.getInstance();
phoneNumberUtil = PhoneNumberUtil.getInstance();
}
public void submit(View v) {
phoneNumber = mEditNumber.getText().toString().trim();
boolean isValidnumber = GeoUtil.checkPhoneNumber(this,phoneNumber);
String carrier = GeoUtil.getCarrier(this,phoneNumber);
location = GeoUtil.getGeocodedLocationFor(this, phoneNumber);
Intent intent = new Intent(this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putBoolean("ifValidNumber",isValidnumber);
bundle.putString("location", location);
bundle.putString("carrier",carrier);
intent.putExtras(bundle);
startActivity(intent);
}
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 手机号码归属地相关推荐
- Android libphonenumber Demo 手机号码归属地
libphonenumber 是google 开源的库,提供手机号码格式化,来电归属地,运营商等多种功能十分强大,现在做个简单的demo 1.首先下载 libphonumber 相关的库 here, ...
- Android系统架构-[Android取经之路]
摘要:本节主要来讲解Android的系统架构 阅读本文大约需要花费10分钟. 文章首发微信公众号:IngresGe 专注于Android系统级源码分析,Android的平台设计,欢迎关注我,谢谢! 欢 ...
- android 服务端技术,移动应用服务器端开发(基于JSP技术)-2017 Android系统构架 Android系统构架.docx...
Android系统构架 PAGE 1 目 录 TOC \o "1-3" \h \z \u 一.Android系统构架 1 二.Linux内核层 2 三.系统运行库层 3 (一)系统 ...
- 【android系统】android系统升级流程分析(二)---update升级包分析
接下来我们将通过几篇文章来分析update.zip包在具体Android系统升级的过程,来理解Android系统中Recovery模式服务的工作原理.今天让我先来分析下升级包update.zip. 一 ...
- 【android系统】android系统升级流程分析(一)---recovery模式中进行update包升级流程分析
今天我们直接来看下android中具体的升级过程是如何的. 升级流程概述 升级的流程图: 升级流程分析 第一步:升级包获取 升级获取可以通过远程下载,也可直接拷贝到指定目录即可. 第二步:准备升级 然 ...
- android log抓取方法,Android系统之Android抓取各种log的方法
Android系统之Android抓取各种log的方法 2018年11月25日 | 萬仟网移动技术 | 我要评论 android之android抓取各种log的方法 1.logcat (四类log b ...
- Android系统(62)-----Android 7.1 新特性之 Shortcuts 介绍
Android 7.1 新特性之 Shortcuts 介绍 Android 7.1 允许 App 自定义 Shortcuts,类似 iOS 的 3D touch.通过在桌面长按 App 弹出 Shor ...
- android系统语音合成,android 语音合成报错
发现了2个问题 第一个貌似是复制离线的资源出错了(已经核对过读写等权限): 12-19 19:54:49.739 32006-32159/com.zhanglf.youxuanz I/NonBlock ...
- Android 系统(71)---Android系统build.prop文件生成过程
Android系统build.prop文件生成过程 Android系统build.prop生成过程 这个文件类似于windows的注册表文件,定义了系统初始的一些参数属性,功能的开放等,通过调整或增加 ...
最新文章
- Python函数参数的五种类型
- 关于C#中的类访问修饰符
- windows下cipher和efsdump工具的初步使用
- COJ 1170 A Simple Problem
- MySQL必知必会(使用子查询)
- C#面向对象2 静态类、静态成员的理解
- python面值组合_算法题 - 拼凑面额 - Python
- 使用AirDrop将文件从iPhone或iPad传送到Mac电脑教程
- JFrame+JButton简单使用(菜鸟入门)——JAVA
- h2ouve下载 insyde_H20UVE_100.00.9.2 Insyde H2OUVE (UEFI Variable Editor) - 下载 - 搜珍网
- 人工智能历史回眸:达特茅斯会议
- 高次同余方程式的解数及解法
- 手机服务器 微信QQ,玩家天价买服务器语聊开黑 小白没想明白:微信QQ难道不行?...
- 信息时代,书香更宜人
- 布线光纤方面的知识都在这了,千万别错过!
- Cg学习记录003 之Varying参数
- Python matplot画柱状图(一)
- VSTO Office二次开发键盘鼠标钩子使用整理
- SpringBoot+Thymeleaf模板实现中英文页面文字翻译
- 【无标题】codesys与rte关系
热门文章
- (4)散列函数设计:除留余数法
- 从Nand Flash启动U-BOOT的基本原理
- [设计模式] - 工厂模式
- 嵌入式Linux系统编程学习之四Shell编程
- linux写文件操作同步,linux 可执行文件与写操作的同步问题(文件读写操作产生的锁机制)...
- 【LeetCode】剑指 Offer 13. 机器人的运动范围
- RPC与RMI的区别
- 不加载,手动实例化Service
- CentOS升级Python到2.7版本
- Fatal error: Call to undefined function: mysql_connect() 的解决