2011-12-16 36 views
0

我正在尋找一種方法來獲取電話號碼或名稱中的收藏夾列表中的聯繫人服務內容無關緊要。誰能幫我這個?如何閱讀服務中的收藏夾聯繫人?

這不是重要的是使用與此相關的代碼

我在developer.android.com像這樣(IN_VISIBLE_GROUP)發現的任何代碼。 如何在我的情況下使用這個變量?

case (PICK_CONTACT): 
       if (resultCode == Activity.RESULT_OK) { 
         Uri contactData = data.getData(); 
         Cursor c = managedQuery(contactData, null, null, null, null); 
         ContentResolver cr = getContentResolver(); 
         if (c.moveToFirst()) { 
           String name = c.getString(c.getColumnIndexOrThrow(People.NAME)); 
           String id =c.getString(c.getColumnIndexOrThrow(People._ID)); 
           Cursor phones = cr.query(Phone.CONTENT_URI, null, 
             Phone.CONTACT_ID + " = " + id, null, null); 
} 

回答

2

讓我們假設如果你想獲得所有可能接觸的收藏價值,在給定的代碼刪除選擇參數,您正在搜索的名字.. 接觸。

//首先得到的顯示名稱聯繫ID爲: -

String displayName = "Albert Einstein"; 

Uri contacts = ContactsContract.Contacts.CONTENT_URI; 

cur = cr.query(contacts, null, ContactsContract.Contacts.DISPLAY_NAME +"="+displayName,null, null); 

int contactIdIndex = cur.getColumnIndex(ContactsContract.PhoneLookup._ID); 

int contactId = cur.getInt(contactIdIndex); 

//Make a query to get the Starred value:- 

Cursor starred = cr.query(ContactsContract.Contacts.CONTENT_URI, 
new String[] { ContactsContract.Contacts.STARRED }, 
ContactsContract.Contacts._ID + " = " + contactId, 
null, null); 

if (starred != null && starred.moveToFirst()) 
{ 
int fav = starred.getInt(0); 
} 

if (starred != null) 
starred.close();    
} 

可以刪除獲得聯繫ID,然後查詢出演價值,並直接查詢基於顯示名稱的步驟

+0

一個100000000000000000謝謝 :) 我可以做這個家庭組? 如果我可以你可以請告訴我如何 :) – 1093822 2011-12-17 20:46:15

0

像這樣的事情?

final private static class DataQuery { 
    public static final int COLUMN_MIMETYPE = 1; 
    public static final int COLUMN_PHONE = 2; 
    public static final int COLUMN_RAWCONTACT_ID = 3; 
    public static final int COLUMN_PHONE_NUMBER = COLUMN_DATA1; 
    public static final String[] PROJECTION = new String[] { Data._ID, Data.MIMETYPE, Data.DATA1, Data.RAW_CONTACT_ID }; 

    public static final String SELECTION_PHONE = Data.DATA1 + "=?"; 
} 

long findContact(Context context, String number) { 
    long rawContactId = -1; 
    final Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION_PHONE, new String[] { number }, null); 
    try { 
     if (cursor.moveToFirst()) { 
      rawContactId = cursor.getLong(DataQuery.COLUMN_RAWCONTACT_ID); 
     } 
    } finally { 
     if (cursor != null) 
      cursor.close(); 
    } 
    return rawContactId; 
} 
+0

感謝您的答覆,但它沒有作用 我有電話號碼和聯繫人姓名和聯繫ID ,但我只是想知道,以檢查此人魔女組他屬於 :( – 1093822 2011-12-16 19:43:31

0

好吧,讓我們嘗試用這個...

private static final Uri DATAGROUP_CONTENT_URI = ContactsContract.Data.CONTENT_URI.buildUpon().appendQueryParameter(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE).build(); 

public static void querytGroups(Context context) { 
    final ContentResolver resolver = context.getContentResolver(); 
    long groupid=getGroupId(resolver, "Family"); 

    final Cursor c = resolver.query(DATAGROUP_CONTENT_URI, DataQueryForContactsInGroup.PROJECTION, DataQueryForContactsInGroup.SELECTION, new String[] {ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupid)}, null); 

    try { 
     while (c.moveToNext()) { 
      final long rawContactId = c.getLong(DataQueryForContactsInGroup.RAW_CONTACT_ID); 
      //do something 
     } 
    }finally { 
     c.close(); 
    } 
} 

private static long getGroupId(final ContentResolver resolver, String groupName) { 
    long groupid = -1; 
    Cursor cur = null; 
    try { 
     cur = resolver.query(Groups.CONTENT_URI, DataQueryForGroup.PROJECTION, DataQueryForGroup.SELECTION, new String[]{"%"+groupName+"%"}, null);   
     while (cur.moveToNext()) { 
      return groupid= cur.getLong(DataQueryForGroup.GROUP_ID); 
     } 
    }finally { 
     if (cur!=null) cur.close(); 
    } 
    return groupid; 
} 

private interface DataQueryForGroup { 
    public final static String[] PROJECTION = new String[] {Groups._ID}; 
    public static final String SELECTION = Groups.TITLE+" LIKE ?"; 
    public final static int GROUP_ID = 0; 
} 

private interface DataQueryForContactsInGroup { 
    public final static String[] PROJECTION = new String[] { Data.RAW_CONTACT_ID }; 
    public static final String SELECTION = "("+Data.MIMETYPE + "=?) and ("+ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+ "=?)"; 
    public final static int RAW_CONTACT_ID = 0; 
} 

請考慮,如果你的谷歌帳戶不是英語,你需要尋找適當的組名

+0

1000000000000000000000000000000000謝謝:)你是真棒男人:) – 1093822 2011-12-19 20:41:07