无法加载联系人

时间:2012-11-04 11:39:21

标签: android load contacts

我已尝试过多个可用示例,这些示例有助于从手机加载联系人。它在模拟器上工作正常但是当我尝试实时移动时它会崩溃。任何人都可以向我发送经过测试的代码,它完美无瑕。然后我可以与我的代码进行比较。

一个失败的代码示例。

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
if (phones.getCount() > 0)
{
    while (phones.moveToNext())
    {
            name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

    }
phones.close();

请帮助。

2 个答案:

答案 0 :(得分:0)

首先,在清单文件中添加:

  <uses-permission android:name="android.permission.READ_CONTACTS"/>

到您的AndroidManifest.xml文件,然后您可以循环访问您的手机通讯录:

     Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
      while (cursor.moveToNext()) { 
      String contactId = cursor.getString(cursor.getColumnIndex( 
       ContactsContract.Contacts._ID)); 
       String hasPhone =  cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
       if (Boolean.parseBoolean(hasPhone)) { 
     // You know it has a number so now query it like this
       Cursor phones = getContentResolver().query(    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,   ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
         while (phones.moveToNext()) { 
        String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));                 
         } 
      phones.close(); 
         }

        Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,  ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 
         while (emails.moveToNext()) { 
      // This would allow you get several email addresses 
       String emailAddress = emails.getString( 
        emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
        } 
        emails.close();
          }
       cursor.close(); 

答案 1 :(得分:0)

确保phones不是null。如果没有null添加此行以将指针移动到第一个电话:

cursor.moveToFirst();

以下是代码片段作为示例,它应该如何工作:

Uri contactData = data.getData();
    //Cursor cursor =  managedQuery(contactData, null, null, null, null);
    Cursor cursor =  cr.query(contactData, null, null, null, null);
    cursor.moveToFirst();
    String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
    String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));



    if (Integer.parseInt(cursor.getString(
            cursor.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 number = pCur.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

            break; // ? we want only 1 value
        } 
        pCur.close();
    }

    cursor.close();

假设它会帮助你