ContactsContract.CommonDataKinds.Phone.NUMBER不能如期工作。

问题描述 投票:0回答:1

我一直在寻找一种非常快速的方法来加载我的安卓应用程序中的联系人。我遇到了 本回答 它的工作!现在我的联系人加载速度超快。

但是只有一个问题,解决办法是使用下面的数组来查询联系人。

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. 我希望能够检测到没有国家代码的电话号码。

我该怎么做呢?

android android-contacts
1个回答
0
投票

你可以使用Google的libphonenumber库来给手机添加remove国家代码,请看这里。https:/github.comgooglelibphonenumber。 (关于在Android中使用的readme)

这里有一个适用于你的案例的相关片段。

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"));```
© www.soinside.com 2019 - 2024. All rights reserved.