简体   繁体   中英

How to get Group ID of contacts Number (Android)

How to get Group ID knowing contact's Number

I guess it have to be another query inside this one, but I have not ANY idea how to do it

Here what I've tried:

String[] projection = new String[]{ ContactsContract.Groups._ID };

Cursor cursor = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        projection,
        ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ number,
        null,
        null);

cursor.moveToNext();
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups._ID));

(Query returns nothing)

Thanks for help!

Your query returns nothing, because there is no such column Groups._ID within dataset CommonDataKinds.Phone .

Try something like this:

String sPhoneNumber = "+48123456789";    
Cursor cursor = getApplicationContext().getContentResolver().query(
                Data.CONTENT_URI, new String[] {CommonDataKinds.GroupMembership._ID}, 
                ContactsContract.CommonDataKinds.Phone.NUMBER+"='"+sPhoneNumber+"'", null, null);

where sPhoneNumber is String with desired phone number. Keep in mind, that your cursor may still return 0 depending on:

  1. how your phone number is formatted, ie you wish to find group ID for contact of given number +49123456789 while number is formatted like this +49 123 456 789 . Whitespaces are making this completely different String .
  2. your contact does not belong to any group.

Also cursor can still return more results, mostly in cases when your contact belongs to more than one group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM