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


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/,之后在终端下看到这个: 
    生成jar包

  2. 找到对应的jar包

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

  3. 导入到工程里

    我用的是Android Studio,也希望也用这个工具,毕竟谷歌推荐用这款软件。 
    首先,复制到libs下: 
    复制到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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值