获取收藏夹联系人列表始终为空

时间:2014-03-25 04:37:44

标签: android contacts favorites

我试图在三星galaxy S4上获得所有联系人相同的收藏夹联系人列表。我正在使用以下代码:

public void getFavoritesContactsList(ContentResolver cr) {


        Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.STARRED};

        String selection =ContactsContract.Contacts.STARRED + "="+"1";

        Cursor cursor = managedQuery(queryUri, projection, selection, null, null);

        while (cursor.moveToNext()) {
            String contactID = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));

            Intent intent = new Intent(Intent.ACTION_VIEW);
            Uri uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
            intent.setData(uri);
            String intentUriString = intent.toUri(0);

            String title = (cursor.getString(
                cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

            name1.add(title);
            _idd.add(contactID);
        }

        cursor.close();
     }

但结果总是为空。

1 个答案:

答案 0 :(得分:0)

public Map getFavoriteContacts(){

    Map contactMap = new HashMap();

    Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.STARRED};

    String selection =ContactsContract.Contacts.STARRED + "='1'";

    Cursor cursor = managedQuery(queryUri, projection, selection, null, null);

    while (cursor.moveToNext()) {
        String contactID = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.withAppendedPath(
            ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
        intent.setData(uri);
        String intentUriString = intent.toUri(0);

        String title = (cursor.getString(
            cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

        contactMap.put(title,intentUriString);
    }

    cursor.close();
    return contactMap;
}