Android联系人内容提供商有时会返回空电话号码

时间:2015-03-04 01:16:50

标签: 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},
                                    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);

我要做的是:

在这里,我通过内容提供商查询来自通讯录的联系人姓名电话号码电子邮件,并将这三者连接成一个字符串,用逗号将它们分隔在字符串中。

问题:

我收到了联系人姓名和电子邮件,没有任何问题。 但是,当我从联系人中选择某些联系人时,返回的电话号码为空

返回空电话号码的联系人是来自其他国家/地区的电话号码,来自我当前的位置。

我现在在加拿大,当我选择加拿大联系人时,我正确地得到了电话号码。

但是,当我从印度选择电话号码时,不会返回该号码,相反,我会得到空的结果。

当我选择与#34; 12345"等数字联系时,会发生同样的情况。或" 5555555555"等。

我的问题

如果发现电话号码无效,可能是Android正在尝试验证号码的真实性并返回空号码吗? *(可能不是!一定是我的错!)*

我该如何解决这个问题?我无法获得电话号码的值,就像用户在联系人中保存的那样?

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

1 个答案:

答案 0 :(得分:2)

我从该代码中观察到的一件事是,您正在使用

阅读联系人
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();
        }

    }