使用CursorLoader仅查找设备联系人

时间:2016-02-18 07:15:02

标签: android contactscontract android-cursorloader

我正在创建一个使用联系人显示的应用程序。我的问题是,我可以显示所有的条件(来自我的电子邮件帐户,Skype和其他),但我想只显示我的设备中的联系人(我现在正在使用的图像),就像打开本机联系人应用程序并转到联系人一样 - >菜单 - >设置 - >联系人 - >联系人显示 - > 设备联系人。我不是很了解联系内容提供商并需要帮助。 stackoverflow上有很多帖子,但我找不到符合我要求的帖子。 对于retreive所有联系我正在使用这个CursorLoader

CursorLoader cL = new CursorLoader(getActivity(),

                contentUri,
                ContactsQuery.PROJECTION,
                ContactsQuery.SELECTION,
               null,
                ContactsQuery.SORT_ORDER
        );

这是我的标准和预测:

 final static int QUERY_ID = 1;

// A content URI for the Contacts table
final static Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;

// The search/filter query Uri
final static Uri FILTER_URI = ContactsContract.Contacts.CONTENT_FILTER_URI;

// The selection clause for the CursorLoader query. The search criteria defined here
// restrict results to contacts that have a display name and are linked to visible groups.
// Notice that the search on the string provided by the user is implemented by appending
// the search string to CONTENT_FILTER_URI.
@SuppressLint("InlinedApi")
final static String SELECTION =
        (Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME) +
                "<>''" + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "= 1 AND "+ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";


// The desired sort order for the returned Cursor. In Android 3.0 and later, the primary
// sort key allows for localization. In earlier versions. use the display name as the sort
// key.
@SuppressLint("InlinedApi")
final static String SORT_ORDER =
        Utils.hasHoneycomb() ? ContactsContract.Contacts.SORT_KEY_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME;

// The projection for the CursorLoader query. This is a list of columns that the Contacts
// Provider should return in the Cursor.
@SuppressLint("InlinedApi")
final static String[] PROJECTION = {

        // The contact's row id
        ContactsContract.Contacts._ID,

        // A pointer to the contact that is guaranteed to be more permanent than _ID. Given
        // a contact's current _ID value and LOOKUP_KEY, the Contacts Provider can generate
        // a "permanent" contact URI.
        ContactsContract.Contacts.LOOKUP_KEY,

        // In platform version 3.0 and later, the Contacts table contains
        // DISPLAY_NAME_PRIMARY, which either contains the contact's displayable name or
        // some other useful identifier such as an email address. This column isn't
        // available in earlier versions of Android, so you must use Contacts.DISPLAY_NAME
        // instead.
        Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME,

        // In Android 3.0 and later, the thumbnail image is pointed to by
        // PHOTO_THUMBNAIL_URI. In earlier versions, there is no direct pointer; instead,
        // you generate the pointer from the contact's ID value and constants defined in
        // android.provider.ContactsContract.Contacts.
        Utils.hasHoneycomb() ? ContactsContract.Contacts.PHOTO_THUMBNAIL_URI : ContactsContract.Contacts._ID,

        // The sort order column for the returned Cursor, used by the AlphabetIndexer
        SORT_ORDER,
        ContactsContract.Contacts.HAS_PHONE_NUMBER
};

1 个答案:

答案 0 :(得分:0)

过了一段时间,我回到搜索这个问题的答案。 答案可能不正确,它可以帮助我。

public static boolean getConnections(Context context, String contactId) {
    // Read all data for contactId
    String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
    String[] selectionArgs = new String[]{contactId};

    Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, selection, selectionArgs, null);
    String accountType = null;
    while (c.moveToNext()) {
        accountType = c.getString(c.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_TYPE));
    }
    Log.e("getConnections", accountType + "");
    c.close();
    if (accountType.equals("SIM Account") || accountType.equals("vnd.sec.contact.phone"))
        return true;
    else
        return false;

}

我的查询发布后,我通过contactId过滤联系人并检查他们的ACCOUNT_TYPE,在我的日志中我看到samsung vnd.sec.contact.phone(意思是电话联系)和我的lg 2 sim发现这个SIM帐户。 也许有更好的解决方案,但我这样解决。