-2

I'm trying to design a broadcast receiver for contacts, means it opens my app when other apps or the user wants any contact details. is there any means to do it(i didnt find it in intent filter). or i have to design my own?

basically when any app is trying to access a contact, that request goes through my app. it is just like BroadcastReceiver for change detection in contacts, I want it for when app wants to access a contact.

  • 1
    Please edit your question and explain, in detail, what "it open my app when other apps or the user wants any contact details" means. Also, please explain what a broadcast has to do with any of this. – CommonsWare Nov 02 '17 at 12:54

1 Answers1

0

What you should do:

BroadcastReceivers are not designed for getting info back, instead you can implement a pattern just like the Contacts APIs do it, by implementing your own ContentProvider.

Create a ContentProvider in your app, to allow other apps to query on your ContentProvider just like they do on the Contacts ContentProvider.

See tutorial.

You need to design how you want your content uris to look like, for example:

vnd.android.cursor.item/my_app/contact to get info about a single contact

And you should also consider adding custom permission to your <provider> in your manifest, so also permitted apps can access your data.

Original Answer:

If you already have an Activity for viewing a contact, in your AndroidManifest, add the following intents:

<activity 
   android:name="..." 
   ...>

            <intent-filter>
                <action android:name="com.android.contacts.action.QUICK_CONTACT" />
                <action android:name="android.provider.action.QUICK_CONTACT" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.item/contact" />
                <data android:mimeType="vnd.android.cursor.item/person" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.item/person" />
                <data android:mimeType="vnd.android.cursor.item/contact" />
                <data android:mimeType="vnd.android.cursor.item/raw_contact" />
            </intent-filter>

</activity>

You'll need to properly handle incoming intents with those mimetypes and data uris in your Activity.

You can an example in the Android system Contacts app manifest: https://android.googlesource.com/platform/packages/apps/Contacts/+/master/AndroidManifest.xml#307

You can test your implementation by adding a widget called "Contact 1x1" (the name may vary depending on the device) to your homescreen, and selecting which contact the widget should launch. When clicking this widget it should call one of the above intents.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • my question is wrong, I understand your answer. I want to replace contactsContract with my app. so that they should not use cursor to obtain data, instead they can create a intent and receive the details. – Suyash Shrivastava Nov 02 '17 at 15:59
  • ok, i get it, so you're gonna offer other apps in the phone to get contacts information using your app? what would that intent look like, like the one in my answer? – marmor Nov 02 '17 at 17:55