How to share a contact programatically in android

梦想的初衷 提交于 2020-02-29 03:12:15

问题


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?


回答1:


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);

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

UPDATE

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);
}



回答2:


If you asking how to share it to something like whatsapps you could try sending it and an intent.

You can send plain text like this:

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
try {
    activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
    ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

See Share image and text through Whatsapp or Facebook for more details.

If you want to go further then that, I suggest you take a look at the thirds party development docs like for WhatsApp

Also more general information can be found on this page of Android

Here is an overview of what you can send in an intent.




回答3:


Use following Code to share your contact, get Contact Name and LookupKey from contact cursor

private void shareContact() {
    //lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
    String lookupKey2 = lookupKey;
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey2);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType(ContactsContract.Contacts.CONTENT_VCARD_TYPE);
    intent.putExtra(Intent.EXTRA_STREAM, uri );
    intent.putExtra(Intent.EXTRA_SUBJECT, contactName); // put the name of the contact here
    startActivity(intent);
}


来源:https://stackoverflow.com/questions/41505718/how-to-share-a-contact-programatically-in-android

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