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 工具类

 
  1. public class GeoUtil {

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

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

  5.  
  6.     // 获取国家码 “CN”

  7. public static String getCurrentCountryIso(Context context) {

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

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

  10. }

  11.  
  12.     //获取归属地信息

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

  14.  
  15. final PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();

  16.  
  17.  
  18. Phonenumber.PhoneNumber structuredNumber = getStructedNumber(context,phoneNumber);

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

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

  21.  
  22. }

  23.  
  24.    //检查是否为 有效号码

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

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

  27. }

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

  30. try {

  31. final Phonenumber.PhoneNumber structuredNumber =

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

  33. return structuredNumber;

  34. } catch (NumberParseException e) {

  35. return null;

  36. }

  37. }

  38.  
  39.     //获取运营商信息

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

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

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

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

  45. }

  46. }

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

 
  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.  
  9. private final String DEFAULT_COUNTRY_ISO = "CN";

  10.  
  11. public static class LocaleProvider {

  12. public Locale getDefaultLocale() {

  13. return Locale.getDefault();

  14. }

  15. }

  16.  
  17. private CountryDetector(Context context) {

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

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

  20. new LocaleProvider());

  21. }

  22. private CountryDetector(Context context, TelephonyManager telephonyManager,

  23. LocationManager locationManager, LocaleProvider localeProvider) {

  24. mTelephonyManager = telephonyManager;

  25. mLocationManager = locationManager;

  26. mLocaleProvider = localeProvider;

  27. mContext = context;

  28. }

  29. public static CountryDetector getInstance(Context context) {

  30. if(sInstance == null) {

  31. sInstance = new CountryDetector(context);

  32. }

  33. return sInstance;

  34. }

  35.  
  36. public String getCurrentCountryIso() {

  37. String result = null;

  38. if (isNetworkCountryCodeAvailable()) {

  39. result = getNetworkBasedCountryIso();

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

  41. }

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

  43. result = getSimBasedCountryIso();

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

  45. }

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

  47. result = getLocaleBasedCountryIso();

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

  49. }

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

  51. result = DEFAULT_COUNTRY_ISO;

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

  53. }

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

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

  56. }

  57.  
  58. /**

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

  60. */

  61. private String getNetworkBasedCountryIso() {

  62. return mTelephonyManager.getNetworkCountryIso();

  63. }

  64.  
  65. /**

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

  67. */

  68. private String getSimBasedCountryIso() {

  69. return mTelephonyManager.getSimCountryIso();

  70. }

  71.  
  72. /**

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

  74. */

  75. private String getLocaleBasedCountryIso() {

  76. Locale defaultLocale = mLocaleProvider.getDefaultLocale();

  77. if (defaultLocale != null) {

  78. return defaultLocale.getCountry();

  79. }

  80. return null;

  81. }

  82.  
  83. private boolean isNetworkCountryCodeAvailable() {

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

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

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

  87. }

  88. }

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.  
  7. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

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

  9. @Override

  10. public void onClick(View view) {

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

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

  13. }

  14. });

  15.  
  16. mEditNumber = (EditText) findViewById(R.id.number);

  17. geocoder = PhoneNumberOfflineGeocoder.getInstance();

  18. phoneNumberUtil = PhoneNumberUtil.getInstance();

  19. }

  20.  
  21.  
  22. public void submit(View v) {

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

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

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

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

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

  30. Bundle bundle = new Bundle();

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

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

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

  34. intent.putExtras(bundle);

  35. startActivity(intent);

  36. }

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

猜你喜欢

转载自blog.csdn.net/zhangbijun1230/article/details/81414338