ContactsContract.CommonDataKinds.Phone.NUMBER不能按预期工作

我一直在寻找一种在我的android应用程序中加载联系人的快速方法。我碰到了this answer,它奏效了!我的联系人现在加载速度非常快。

但是只有一个问题,解决方案包括使用下面的数组查询联系人:

private static final String[] PROJECTION = new String[] {
        ContactsContract.CommonDataKinds.Phone.CONTact_ID,ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMber
};

很显然,ContactsContract.CommonDataKinds.Phone.NUMber检索电话号码。

我当前面临的问题是该字段仅检索带有国家/地区代码的电话号码。因此,它可以轻松检测到 +2347000000000 ,而不能检测到 07000000000 。我希望能够检测到没有国家代码的电话号码。

我该怎么办?

jackychen0116 回答:ContactsContract.CommonDataKinds.Phone.NUMBER不能按预期工作

您可以使用Google的libphonenumber库来向电话添加/删除国家/地区代码,请参见此处: https://github.com/google/libphonenumber(阅读有关Android中使用情况的自述文件)

以下是与您的用例相关的代码段:

There are a few formats supported by the formatting method,as illustrated below:

// Produces "+41 44 668 18 00"
System.out.println(phoneUtil.format(swissNumberProto,PhoneNumberFormat.INTERNATIONAL));
// Produces "044 668 18 00"
System.out.println(phoneUtil.format(swissNumberProto,PhoneNumberFormat.NATIONAL));
// Produces "+41446681800"
System.out.println(phoneUtil.format(swissNumberProto,PhoneNumberFormat.E164));
You could also choose to format the number in the way it is dialed from another country:

// Produces "011 41 44 668 1800",the number when it is dialed in the United States.
System.out.println(phoneUtil.formatOutOfCountryCallingNumber(swissNumberProto,"US"));```
本文链接:https://www.f2er.com/2383140.html

大家都在问