您的位置:首页 > 编程语言 > Go语言

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

2016-10-21 19:25 681 查看

前言

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

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

好了,大家稍安勿躁,我不是来吐槽的,我是来提供解决方法的,大家接着看下去。

我认为最好的办法就是用Google的公共电话号码解析库—libphonenumber

这个库Android源码的路径是:

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


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

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

将libphonenumber作为module放到你的工程里

将libphonenumber作为jar放到Android工程的libs目录下

步骤

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

编译libphonenumber

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



找到对应的jar包

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

导入到工程里

我用的是Android Studio,也希望也用这个工具,毕竟谷歌推荐用这款软件。

首先,复制到libs下:



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

compile files('libs/libphonenumber.jar')


如图:



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

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


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;
}


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

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;
}

}


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

String address = CallerInfo.getGeoDescription(mContext,number);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息