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

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

问题描述

我正在使用 Android 2.0 并尝试接收所有联系人的列表.

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

由于 android.provider.Contacts.People 已弃用,我必须使用 android.provider.ContactsContract,但我找不到正确的示例来说明如何使用使用它(例如:检索电话簿上所有联系人的列表).

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?

推荐答案

首先确保你已经添加了

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

到你的 AndroidManifest.xml 文件,然后你可以像这样循环你的手机联系人:

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

此外,如果您需要从联系人处获取诸如备注之类的内容,那么您将需要使用不同的 URI,如下所示(您可以随意使用此方法):

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

请注意,这次我不仅使用了联系人 ID,还使用了查询的 MIME 类型.

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

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

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