How to read contacts on Android 2.0

前端 未结 9 980
我寻月下人不归
我寻月下人不归 2020-11-22 05:58

I\'m working on Android 2.0 and am trying to receive a list of all contacts.

Since android.provider.Contacts.People is deprecated, I have to use android

9条回答
  •  难免孤独
    2020-11-22 06:29

    First, ensure that you have added

    
    

    to your AndroidManifest.xml file, then you can loop through your phone contacts like this:

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

    Additionally, you can loop through your contacts and simply get the name and phone number like this:

    Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    
    while(people.moveToNext()) {
       int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
       String contact = people.getString(nameFieldColumnIndex);
       int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
       String number = people.getString(numberFieldColumnIndex);
    }
    
    people.close();
    

    Furthermore, if you need to get things like notes from a contact then you will need to use a different URI, like the following (feel free to use this method):

    private String getNote(long contactId) { 
       String note = null; 
       String[] columns = new String[] { ContactsContract.CommonDataKinds.Note.NOTE }; 
       String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
       String[] whereParameters = new String[]{Long.toString(contactId), ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; 
       Cursor contacts = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null); 
       if (contacts.moveToFirst()) { 
          rv = contacts.getString(0); 
       } 
       contacts.close(); 
       return note; 
    } 
    

    Notice this time I used not only the contact id but the mime type for the query.

提交回复
热议问题