我有一个代码,使用内容解析器从移动联系人中获取电话号码。我在2个手机上测试了它。在一个中它获取国家代码而在另一个中它没有。是否有办法找出国家代码是否存在于手机号码,如果有的话我想把它与号码分开。这是我的代码。请帮帮我
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
phoneContactName.add(name);
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
String countryCode = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneContactNos.add(phoneNo);
dup_phoneContactNos.add(phoneNo);
/*if (registeredContactNos.contains(phoneNo)) {
Toast.makeText(getApplicationContext(), "match found", Toast.LENGTH_SHORT).show();
selectedContactName.add(name);
selectedContactNos.add(phoneNo);
populateList(name, phoneNo);
}*/
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
/*for(int i=0;i<selectedContactName.size();i++)
{
Toast.makeText(getApplicationContext(),selectedContactName.get(i)+","+selectedContactNos.get(i),Toast.LENGTH_SHORT).show();
}*/
}
if (cur != null) {
cur.close();
}
你可以使用Android的PhoneNumberUtils.formatNumber
。
如果您只定位Lollipop及以上设备:
String formattedPhone = PhoneNumberUtils.formatNumber(phone, Locale.getDefault().getCountry()));
如果您还定位旧版本:
String formattedPhone;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
formattedPhone = PhoneNumberUtils.formatNumber(phone, Locale.getDefault().getCountry()));
} else {
formattedPhone = PhoneNumberUtils.formatNumber(phone));
}
Getting telephone country code with Android此链接适用于我。我发布了所有寻找像我一样的答案的链接