ContactsContract.CommonDataKinds.Phone.NUMBER 没有按预期工作
ContactsContract.CommonDataKinds.Phone.NUMBER does not work as expected
我一直在寻找一种在我的 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
检索到 phone 号码。
我目前面临的问题是该字段仅检索 phone 带有国家代码的号码。因此,它可以很容易地检测到 +2347000000000 而不是 07000000000。我希望能够检测 phone 没有国家代码的号码。
我该怎么办?
您可以使用 Google 的 libphonenumber 库将 add/remove 国家代码添加到电话中,请参见此处:
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"));```
我一直在寻找一种在我的 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
检索到 phone 号码。
我目前面临的问题是该字段仅检索 phone 带有国家代码的号码。因此,它可以很容易地检测到 +2347000000000 而不是 07000000000。我希望能够检测 phone 没有国家代码的号码。
我该怎么办?
您可以使用 Google 的 libphonenumber 库将 add/remove 国家代码添加到电话中,请参见此处: 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"));```