Android 有时联系内容提供商 returns 空 phone 号码

Android Contacts Content Provider sometimes returns empty phone numbers

这是用于获取联系信息的代码:

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);

我想做什么:

在这里,我查询了联系人姓名phone号码邮箱 从 Contacts 通过 content provider 并将所有三个连接成一个字符串,在字符串中用逗号分隔它们。

问题:

我得到了联系人姓名和电子邮件,没有任何问题。 但是,当我select 一些联系人 时,phone 号码returned 是空的

return 空 phone 号码的联系人是来自不同国家/地区的 phone 号码,来自我当前的位置。

我现在在加拿大,当我 select 加拿大联系人时,我正确地得到了 phone 号码。

但是,当我 select phone 来自印度的号码时,该号码未 returned,相反,我得到空结果。

当我 select 使用“12345”或“5555555555”等号码的联系人时,也会发生同样的情况

我的问题

会不会是Android在验证号码的真实性,return输入一个空号码,发现phone号码无效? *(可能不是!一定是我的错误!)*

我该如何解决这个问题?我不能像用户在联系人中保存的那样获取 phone 号码的值吗?

对于代码或我的问题中的任何歧义,我深表歉意。我是一名新手程序员,非常感谢您的所有投入!

我从该代码中观察到的一件事是,您正在阅读联系人而不使用

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();
        }

    }