无法读取联系人的所有值

时间:2009-10-13 08:09:23

标签: java android

我已经应用了从android通讯录中读取所有联系人及其值,但我无法阅读所有联系方式。 目前我只得到它的名字和手机号码。

如何获取所有电话号码,所有eamil值和地址值?

提前致谢...

1 个答案:

答案 0 :(得分:1)

首先,不要在评论中加入代码。编辑您的原始帖子,使其更容易阅读。

其次,这对Android来说有点复杂。联系信息并非全部在同一个地方,因此如果您想要所有,您将不得不执行多个查询。以下是从this thread剪切并粘贴所有电话号码的示例:

void trythiscode(){
   // An array specifying which columns to return.
   String[] projection = new String[] {
   People._ID,
   People.NAME,
   People.NUMBER,
};

   // Get the base URI for People table in Contacts content provider.
   // which is: content://contacts/people/
   Uri contactUri = People.CONTENT_URI;

   // Best way to retrieve a query; returns a managed query.
   Cursor peopleCursor = managedQuery (contactUri,
     projection, //Which columns to return.
     null, // WHERE clause--we won't specify.
     null, // Selection Args??
     People.DEFAULT_SORT_ORDER); // Order-by name

   // go to the beginning of the list
   peopleCursor.moveToFirst();


   // So, here we have a contact. We need to get the contact ID (_id) then
   // build the Uri to get the phones section of that user's record
   // which is a subdirectory of a contact record

   long personId = peopleCursor.getLong(peopleCursor.getColumnIndex("_id"));

   Uri personUri = ContentUris.withAppendedId(contactUri, personId );

   // So now the URL looks like: content://contacts/people/_id(where the actual id of the record is here)
   Uri phoneUri=
    Uri.withAppendedPath(personUri, Contacts.People.Phones.CONTENT_DIRECTORY);

   // Now the URL looks like: content://contacts/people/_id/phones (where phones is literally "phones")

   // Now get all the phone numbers for this contact
   Cursor phonesCursor = managedQuery(phoneUri,
     null,
     null,
     null,
     Phones.DEFAULT_SORT_ORDER);

   // We now have a cursor for all the phone numbers for that User ID
   // go to the beginning of the phone list.
   phonesCursor.moveToFirst();


}

这完全有可能是过时的(我还没试过)。如果他们更新Donut中的内容,您将不得不花一些时间阅读文档。