如何在 Android 2.0 上读取联系人 [英] How to read contacts on Android 2.0

查看:28
本文介绍了如何在 Android 2.0 上读取联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.provider.ContactsContract, But I can't find a proper example of how to use it (ex: retrieve a list of all contacts on the phonebook).

Anyone knows where to find such an example?

解决方案

First, ensure that you have added

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

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.

这篇关于如何在 Android 2.0 上读取联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆