How to show&handle contact details intents of apps?

余生颓废 提交于 2019-11-30 15:20:57

If you query for all that contact's info from the ContactsContract.Data.CONTENT_URI table, and dump it to log, you'll see raw-contacts in accounts like com.whatsapp or com.viber, that have data rows with mimetypes that begin with vnd.android.cursor.item.

For example a Whatsapp Data row might look like this:

_id: 247
account_type: com.whatsapp
mimetype: vnd.android.cursor.item/vnd.com.whatsapp.profile
display_name: Bob
raw_contact_id: 62
data1: 1123456789@s.whatsapp.net
data2: WhatsApp
data3: Message +1 123-456-789
// other info ...

So when your code sees such Data rows, it should display to the user the app icon of the app com.whatsapp (account_type) with the text Message +1 123-456-789 (data3) and you can also display other info like app name Whatsapp (data2).

When that action is clicked, you need to create an intent like this:

Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, 247);
Intent i = new Intent(Intent.ACTION_VIEW, uri);
i.setType("vnd.android.cursor.item/vnd.com.whatsapp.profile");

The app should have an Activity that registered to that mimetype, that will query the Data.CONTENT_URI table for the 247 row-id, get the profile id from data1 and perform the action requested.

The specific fields (which one is the visible text, etc.) are defined in a ContactsDataKind object in the app, but it's not that easily read by external apps, but in my experience most such apps use the same fields for the same behavior (e.g. data3 is the user displayed action text)

P.S.
To get the resource of an app that is not yours, you can use this:

Drawable icon = getPackageManager().getApplicationIcon( PACKAGE_NAME );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!