按MIME类型在Android中获取联系人 [英] Get contacts by MIME type in Android

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

问题描述

我希望用他们的MIME类型在Android中获得的联系人列表。

I want to get a list of contacts based on their MIME type in Android.

例如我想这有电子邮件地址的联系人列表。

For example I want a list of contacts which have email addresses.

推荐答案

您应该读与使用 ContactsContract.RawContacts.Entity 目录。如果原始接触有数据的行,所述实体光标将包含每个数据行的行。如果原始接触没有数据的行,光标将仍包含与原料接触级信息的一行。

You should read a raw contact along with all the data associated with it by using the ContactsContract.RawContacts.Entity directory. If the raw contact has data rows, the entity cursor will contain a row for each data row. If the raw contact has no data rows, the cursor will still contain one row with the raw contact-level information.

Uri rawContactUri =
  ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);

Uri entityUri =
  Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);

Cursor c =
  getContentResolver().query(
    entityUri,
    new String[] {
      RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1 },
    null, null, null);

try {
     while (c.moveToNext()) {
         String sourceId = c.getString(0);
         if (!c.isNull(1)) {
             String mimeType = c.getString(2);
             String data = c.getString(3);
             //decide here based on mimeType, see comment later
         }
     }
} finally {
     c.close();
}

例如,如果 mime类型 Phone.CONTENT_ITEM_TYPE ,则列 DATA1 存储的电话号码,但如果数据类是 Email.CONTENT_ITEM_TYPE ,那么 DATA1 存储的电子邮件地址。

For example, if the mimeType is Phone.CONTENT_ITEM_TYPE, then the column DATA1 stores the phone number, but if the data kind is Email.CONTENT_ITEM_TYPE, then DATA1 stores the email address.

这篇关于按MIME类型在Android中获取联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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