我如何在服务中阅读收藏夹联系人?



我正在寻找一种方法,从电话号码或名称中获得服务内部收藏列表中的联系人,这并不重要。有人能帮我一下吗?

使用与此代码相关的任何代码都不重要

我在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);
}

假设您正在按姓名搜索联系人。如果您想获得所有可能的联系人的favorite值,请在给定的代码中删除selection参数。

//首先从显示名中获取联系人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然后查询星号值的步骤,直接根据显示名称

进行查询。

像这样?

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

好的,让我们试试这个…

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

请考虑,如果你的谷歌帐户不是英文的,你需要寻找合适的组名

最新更新