Android联系人内容提供商有时返回空电话号码
以下是用于获取联系信息的代码:Android联系人内容提供商有时返回空电话号码,android,android-contentprovider,android-contacts,android-cursor,Android,Android Contentprovider,Android Contacts,Android Cursor,以下是用于获取联系信息的代码: String id = data.getData().getLastPathSegment(); Cursor cursor = getActivity().getContentResolver() .query(ContactsContract.Data.CONTENT_URI, new String[] {ContactsContract.Data.DISPLAY_NAME
String id = data.getData().getLastPathSegment();
Cursor cursor = getActivity().getContentResolver()
.query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.Data.DISPLAY_NAME},
ContactsContract.Data.CONTACT_ID + "=?",
new String[]{id},
null);
// short circuit if we didn't pick a contact
if (cursor.getCount() == 0) {
return;
}
String contact = "";
String contactName = "";
//code to get contact name
if (cursor.moveToFirst() && cursor.getString(0) != null) {
contact = contact + cursor.getString(0) + ",";
contactName = cursor.getString(0);
}
else {
contact = contact + ","; //changed
}
cursor.close();
// code to get phone number
cursor = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
newString[{ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[]{id},null);
if (cursor.moveToFirst() && cursor.getString(0) != null) {
contact = contact + cursor.getString(0) + ",";
contact = contact + cursor.getString(0) + ",";
}
else {
contact = contact + ",,"; //changed
}
cursor.close();
//Code to get email
cursor = getActivity().getContentResolver()
.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS},
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
new String[]{id},null);
if (cursor.moveToFirst() && cursor.getString(0) != null) {
contact = contact + cursor.getString(0);
}
cursor.close();
contact = contact.replace("+", "");
Log.i("TAG", "CONTACT INFO = " + contact);
我想做什么:
在这里,我通过内容提供商从联系人中查询联系人姓名、电话号码和电子邮件,并将三者连接成一个字符串,在字符串中用逗号分隔
问题:
我得到了联系人姓名和电子邮件,没有任何问题。
但是,当我从contact中选择一些联系人时,返回的电话号码为空
返回空电话号码的联系人是来自不同国家/地区、我当前所在位置的电话号码
我现在在加拿大,当我选择“加拿大联系人”时,我会正确地获得电话号码
但是,当我选择来自印度的电话号码时,号码不会返回,而是返回空结果
当我选择带有“12345”或“5555”等号码的联系人时,也会发生同样的情况
我的问题
如果Android发现电话号码无效,是否会尝试验证号码的真实性并返回一个空号码*(可能不是!一定是我错了!)*
我如何解决这个问题?我不能像用户在通讯录中保存电话号码一样获取电话号码的值吗
对于代码中的任何明确性或我的问题,我深表歉意。我是一个新手程序员,非常感谢您的所有投入 我从代码中观察到的一点是,您正在使用out读取联系人
ContactsContract.Contacts.CONTENT_URI
我修改了你的代码并创建了一个新方法来打印所有联系人
public static void fetchContacts(Context context) {
String phoneNumber = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
StringBuffer output = new StringBuffer();
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
long hasPhoneNumber = Long.parseLong(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
if (hasPhoneNumber > 0) {
output.append("\n Name:" + name);
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);
while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
output.append("\n Number:" + phoneNumber);
System.out.println("Number:::::"+phoneNumber);
System.out.println("Contact:::::"+output.toString());
}
phoneCursor.close();
}
}
}else{
// Toast.makeText(context, "No contacts Found", Toast.LENGTH_LONG).show();
}
}
我正在尝试运行代码。但是,我无法理解
Util.formatMobileNumber()
。Util来自哪个库?忽略Util.formatMobileNumber(),我用来格式化长度小于8的手机号码。我编辑了我的答案,你现在可以检查它是否有效?我几乎有同样的问题。你能帮帮我吗?