1

次の関数は、入力パラメーターとして電話番号 (+436641234567 または +436641234567 など) を持ち、連絡先データベースで 2 つのルックアップを実行します。最初に、この番号に属するユーザーを識別し (これは既に機能しています)、次にユーザーの ID を使用します。この連絡先が割り当てられているすべてのグループを取得します(これは機能しません)。テストは (再び) 正しい ID を返しますが、グループは「null」です。

    public String getNameGroupByNumber(Context context, String number) {
    String name = "?";
    String contactId = "0";
    String group = "0";
    String test = "0";

    // Step 1: LookUp Name to given Number
    ContentResolver contentResolver = context.getContentResolver();

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] contactProjection = new String[] {BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME };
    Cursor contactLookup = contentResolver.query(uri, contactProjection, null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();

            name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));     
        }
    } finally {         
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    Log.d(TAG, "Name: " +name + " ContactId: " + contactId);    // works as expected

    // Step 2: Lookup group memberships of the contact found in step one
    Uri groupURI = ContactsContract.Data.CONTENT_URI;
    String[] groupProjection = new String[]{ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID , ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID};
    Cursor groupLookup = contentResolver.query(groupURI, groupProjection, ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID+"="+contactId, null, null);

    try {

        if (groupLookup != null && groupLookup.getCount() > 0) {
            groupLookup.moveToNext();

             test = groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID));
             group = groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID));

             Log.d(TAG, "Group found with Id: " + test + " and GroupId: " + group);   // test is again the contactID from above but group is null

        }
    } finally {         
        if (groupLookup != null) {
            groupLookup.close();
        }
    }

    return name;
 }
4

1 に答える 1

3
Cursor groupLookup = getContentResolver().query(
        ContactsContract.Data.CONTENT_URI,
        new String[]{
                ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID , 
                ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID
        },
        ContactsContract.Data.MIMETYPE + "=? AND " + ContactContract.Data.CONTACT_ID + "=?",
        new String[]{
                ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,
                contact_id
        }, null
);

groupLookup代わりにこのカーソルを使用してください。

Cursor groupCursor = getContentResolver().query(
        ContactsContract.Groups.CONTENT_URI,
        new String[]{
                ContactsContract.Groups._ID,
                ContactsContract.Groups.TITLE
        }, ContactsContract.Groups._ID + "=" + _id, null, null
);

groupCursor_id からグループ タイトルを取得するのに役立ちます。

于 2012-12-31T15:58:02.690 に答える