简体   繁体   English

在Android中共享联系人

[英]Sharing contacts in android

Im trying to share a contact using my android app, and Im stocked. 我正在尝试使用我的Android应用共享联系人,并且我有很多东西。 Is there any way to share a contact directly using intents? 有什么方法可以直接使用意图共享联系人? Or do I have to send the contact information and re-build it in the other device? 还是我必须发送联系信息并在另一台设备上重建它? Also, is there any other way to do that without using intents? 另外,还有其他方法可以不使用意图来做到这一点吗?

You can use ContactsContract APIs to share a contact as a vcard file (eg to send via Whatsapp). 您可以使用ContactsContract API将ContactsContract共享为vcard文件(例如,通过Whatsapp发送)。

(I'm assuming you already have the contact's lookupKey, if you don't have it, let me know, and i'll add the code for getting it from contactId) (我假设您已经有了联系人的lookupKey,如果您没有联系人,请告诉我,我将添加用于从contactId获取它的代码)

UPDATE added method to get LookupKey from ContactId UPDATE添加了从ContactId获取LookupKey的方法

(make sure you import Contacts from ContactsContract.Contacts ) (请确保您导入ContactsContactsContract.Contacts

private String getLookupKey(long contactId) {
    String lookupKey = null;
    String[] projection = new String[]{Contacts.LOOKUP_KEY};
    String selection = Contacts._ID + "=" + contactId;
    Cursor c = getContentResolver().query(Contacts.CONTENT_URI, projection, selection, null, null);
    if (c != null) {
        if (c.moveToFirst()) {
            lookupKey = c.getString(0);
        }
        c.close();
    }
    return lookupKey;
}

String lookupKey = getLookupKey(long contactId);
if (TextUtils.isEmpty(lookupKey)) {
    Log.e(TAG, "couldn't get lookupKey");
    return;
}
Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(Contacts.CONTENT_VCARD_TYPE);
intent.putExtra(Intent.EXTRA_STREAM, shareUri);
intent.putExtra(Intent.EXTRA_SUBJECT, "Share a contact");
startActivity(intent);

The strategy I would use would be to fetch the desired contacts locally. 我将使用的策略是在本地获取所需的联系人。 Then, parse these into JSON objects with the desired fields. 然后,将其解析为具有所需字段的JSON对象。 Either send the JSON to a server that the other user downloads from or send the JSON over bluetooth to other device. 将JSON发送到其他用户下载的服务器,或者通过蓝牙将JSON发送到其他设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM