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

查看:157
本文介绍了如何阅读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 的是德precated,我必须使用的 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"/>

你的Andr​​oidManifest.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天全站免登陆