简体   繁体   中英

how to perform a search on work profile contacts

I am facing one problem and not getting solution on internet. I am able to list all user profile contacts but its not showing contacts from work profile.

please refer to below links for detail about work profile https://support.google.com/work/android/answer/6191949?hl=en https://support.google.com/work/android/answer/7029561?hl=en

`

private static final String[] PROJECTION =
        {
            Contacts._ID,
            Contacts.LOOKUP_KEY,
            Build.VERSION.SDK_INT
                    >= Build.VERSION_CODES.HONEYCOMB ?
                    Contacts.DISPLAY_NAME_PRIMARY :
                    Contacts.DISPLAY_NAME

        };

private static final String SELECTION =
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
            Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?" :
            Contacts.DISPLAY_NAME + " LIKE ?";

@Override
    public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
        /*
         * Makes search string into pattern and
         * stores it in the selection array
         */
        mSelectionArgs[0] = "%" + mSearchString + "%";
        // Starts the query
        return new CursorLoader(
                getActivity(),
                Contacts.CONTENT_URI,
                PROJECTION,
                SELECTION,
                mSelectionArgs,
                null
        );
    }
`

For example: i have a contact with name "todd" in normal profile on the other hand i have a "tom" in contact under my work profile. Now in native message app during compose it shows todd and tomm both. i want same to happen in my implementation.

How should i get work profile contacts?

refer to the code below that solved my problem

private static final String[] PROJECTION_ENTERPRISE = new String[]{
            ContactsContract.Contacts._ID,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.DATA1,
            ContactsContract.CommonDataKinds.Phone.MIMETYPE,
            ContactsContract.CommonDataKinds.Phone.TYPE,
            ContactsContract.CommonDataKinds.Phone.LABEL
    };

    @RequiresApi(api = Build.VERSION_CODES.N)
        private Cursor getEnterpriseContact(String searchString, String[] cols, ContactSearchType mContactSearchType, String digits, String sortOrder) {
            // Get the ContentResolver
            ContentResolver cr = mContext.getContentResolver();
            // Get the Cursor of all the contacts
            Uri phoneUri = ContactsContract.CommonDataKinds.Phone.ENTERPRISE_CONTENT_FILTER_URI.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.ENTERPRISE_DEFAULT)).build();
            Uri phoneUriWithSearch = Uri.withAppendedPath(phoneUri, Uri.encode(searchString));
            Cursor pCursor = cr.query(phoneUriWithSearch, cols, null, null, sortOrder);
            Cursor workCur = null;
            if (mContactSearchType != ContactSearchType.CONTACT_WITH_PHONE_NO) {
                Uri emailUri = ContactsContract.CommonDataKinds.Email.ENTERPRISE_CONTENT_FILTER_URI.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.ENTERPRISE_DEFAULT)).build();
                Uri emailUriWithSearch = Uri.withAppendedPath(emailUri, Uri.encode(searchString));
                Cursor eCursor = cr.query(emailUriWithSearch, cols, null, null, sortOrder);
                workCur = new MergeCursor(new Cursor[]{pCursor, eCursor});
            } else {
                workCur=pCur;
            }
     return workCur;
    }

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