2011-06-15 89 views
9

我想要獲取Android聯繫人的收藏夾列表中的所有聯繫人。目前,我可以得到所有的組ID,包括最喜歡的組ID。但似乎沒有聯繫人將組ID標識爲最喜歡的組ID。在Android中獲取收藏夾聯繫人

我試圖讓每組中的所有組ID和聯繫人。打印兩個列表後,我發現,最喜歡的羣組標識是不是在聯繫人列表中

ArrayList<String> favGroupId=new ArrayList<String>(); 
     final String[] GROUP_PROJECTION = new String[] { 
       ContactsContract.Groups._ID, ContactsContract.Groups.TITLE }; 
     Cursor cursor = getContentResolver().query(
     ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null, 
       null, ContactsContract.Groups.TITLE); 

     while (cursor.moveToNext()) { 
      String id = cursor.getString(cursor 
        .getColumnIndex(ContactsContract.Groups._ID)); 
      Log.v("Test",id); 

      String gTitle = (cursor.getString(cursor 
        .getColumnIndex(ContactsContract.Groups.TITLE))); 

      Log.v("Test",gTitle); 
      if (gTitle.contains("Favorite_")) { 
       gTitle = "Favorites"; 
       favGroupId.add(id); 
      } 
     } 
     cursor.close(); 

回答

22

您可以使用在ContactsContract.Contact類的STARRED領域。如果你改變你的查詢:

Cursor cursor = this.managedQuery(
    ContactsContract.Contacts.CONTENT_URI, projection, "starred=?", 
    new String[] {"1"}, null); 

這應該返回出現在Android設備上的默認聯繫人應用程序收藏夾選項卡的所有聯繫人的列表。

4

完整的答案,包括intentUriString用於與意圖打開聯繫人:

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; 
}