如何在Android中以编程方式共享联系人 [英] How to share a contact programatically in android

查看:161
本文介绍了如何在Android中以编程方式共享联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发联系人应用,并且我一直在寻找一段时间以便在Android中以编程方式共享联系人.我不知道我应该以哪种格式将联系人发送到其他设备.如果我以文本形式发送,则如何将其处理为接收方设备中的contactstract Db?你能建议我如何使它工作吗?

I am presently working on a contact app and i have been searching for a while to share a contact programatically in android. I didn't know in which format i should send the contact to other device. If i am sending as text, how it will be processed into contactscontract Db in receivers device? Can you please suggest me how to make it work?

推荐答案

您需要获取联系人的 VCard 句柄(使用 ContactsContract API: Contacts.CONTENT_VCARD_URI ),然后使用 ACTION_SEND 意向将其发送.

You need to get the VCard handle for the contact (using ContactsContract API: Contacts.CONTENT_VCARD_URI), and then send it using the ACTION_SEND intent.

String lookupKey = <the contact's lookup key>;
Uri vcardUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(ContactsContract.Contacts.CONTENT_VCARD_TYPE);
intent.putExtra(Intent.EXTRA_STREAM, shareUri);
intent.putExtra(Intent.EXTRA_SUBJECT, "Bob Dylan"); // put the name of the contact here
startActivity(intent);

在此处查看有关此内容的更多信息: https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#CONTENT_VCARD_URI

See more about it here: https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#CONTENT_VCARD_URI

更新

回答@Abhay Maniyar删除的问题-从contactId获取lookupKey:

to answer a deleted question by @Abhay Maniyar - to get the lookupKey from a contactId:

Cursor cur = getContentResolver().query(Contacts.CONTENT_URI, new String[] { Contacts.LOOKUP_KEY }, Contacts._ID + " = " + contactId, null, null);
if (cur.moveToFirst()) {
    String lookupKey = cur.getString(0);
}

这篇关于如何在Android中以编程方式共享联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆