Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Contact Picker with a Filter

It is easy to open the Android Contact App to show all contacts and pick one of them:

in Activity:

    private int PICK_CONTACT = 853456;

    // ...

    // open contact list
    void openContactPicker() {
        Intent it= new Intent(Intent.ACTION_PICK,  Contacts.CONTENT_URI);
        startActivityForResult(it, PICK_CONTACT);
    }

    // when back from intent: use pick result
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // ...
        switch (requestCode) {
        case PICK_CONTACT:
            if (dataOk(data)) {
                extractContactInfo(data);
            } else {
                showErrorMessage();
            }
            break;
        // ...
    }

But is it possible to set some filter criteria, so that the Contact App will only display those contacts which have specified elements - e.g. a complete postal info, or a proper email, or a telephone number?

My App needs the postal info, the currently implemented work-flow is like that:

  • User clicks button to open contacts
  • Contact App is started, displays all contacts
  • user selects one
  • back in my activity the contact is checked
    • postal info available -> do the right thing
    • postal info not available -> message box

Since many contacts do not have a postal info, in most cases a message box 'sorry no postal info available for this contact' will be shown. This is not an acceptable behavior.

One alternative is - I'm just implementing this - to query the contacts database inside the app and do the filtering in my own code, but using this approach has some implications:

  • the app requires the read contacts permission, which might be a no go for many users
  • a contact picker has to be implemented, which possibly looks different than the one the user is familiar with

So, setting some criteria for the Contacts App seems a much more elegant way of doing this.

The App should run on Android 2.3.3 and higher.

Questions:

  1. Is it possible on 2.3.3 to set filter criteria (especially sth. like 'has_postal_information') for the Contacts App, when starting it via startActivityForResult?
  2. If not: is it possible on later OS versions?
like image 220
Gisela Avatar asked Apr 27 '13 13:04

Gisela


1 Answers

The Contacts app registers to this intent filter

<intent-filter>
    <action android:name="android.intent.action.PICK" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="vnd.android.cursor.dir/contact" />
    <data android:mimeType="vnd.android.cursor.dir/person" />
    <data android:mimeType="vnd.android.cursor.dir/phone_v2" />
    <data android:mimeType="vnd.android.cursor.dir/phone" />
    <data android:mimeType="vnd.android.cursor.dir/postal-address_v2" />
    <data android:mimeType="vnd.android.cursor.dir/postal-address" />
</intent-filter>

So you can create your intent like this:

private int PICK_CONTACT = 853456;

// ...

// open contact list
void openContactPicker() {
    Intent it= new Intent(Intent.ACTION_PICK,  Contacts.CONTENT_URI);
    it.setType("vnd.android.cursor.dir/postal-address");
    startActivityForResult(it, PICK_CONTACT);
}
like image 166
idog Avatar answered Oct 21 '22 19:10

idog