android - 如何通过intent-filter和action.SEND_MULTIPLE接收多张电子名片

标签 android android-intent android-contacts intentfilter vcf-vcard

我尝试通过 Android 联系人应用中的共享功能在我的应用中接收多个联系人,但我只收到第一个联系人,我从未收到 ACTION_SEND_MULTIPLEaction >.

我用过https://developer.android.com/training/sharing/receive作为灵感。

AndroidManifest:

<activity android:name=".ShareActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/x-vcard" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND_MULTIPLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/x-vcard" />
    </intent-filter>
</activity>

分享 Activity :

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equals(action) && type != null) {
    // My code always pass here because action is always equals to ACTION_SEND even with several contacts selectionned
    if ("text/x-vcard".equals(type)) {
        handleSendContact(intent);
    }
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
    if ("text/x-vcard".equals(type)) {
        handleSendMultiContact(intent);
    }
}

private void handleSendContact(Intent intent) {
    Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    // use uri to get lookupkey...
}

private void handleSendMultiContact(Intent intent) {
    ArrayList<Uri> contactUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    for (Uri uri : contactUris) {
        // use uri to get lookupkey...
    }
}

共享一个联系人,一切工作正常,但是当我选择两个或更多联系人时,我收到的操作仍然是 ACTION_SEND 而不是 ACTION_SEND_MULTIPLE 并且我只收到第一个选择的联系人接触。我的目标是接收在 Android 联系人应用程序中选择的所有联系人。

最佳答案

简短版本:

ACTION_SEND_MULTIPLE 绝不会与联系人/vCard 共享一起使用。相反,ACTION_SEND 始终与包含多个联系人的数据和包含由 : 分隔的多个 LOOKUP_KEY 的 uri 一起使用。


长版本:

共享联系人一:

content://com.android.contacts/contacts/as_multi_vcard/2345r1817-373F294D4F4329413151472F294F312F

BEGIN:VCARD
VERSION:2.1
N:NAME_1;
TEL;WORK:039-999-9999
EMAIL;HOME:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1d4d1c5c0d5c4c5e1c9ceccc48fc7d3" rel="noreferrer noopener nofollow">[email protected]</a>
URL:www.home_updated.fr
END:VCARD

共享联系人二:

content://com.android.contacts/contacts/as_multi_vcard/2345r1820-4D553943354B2F514F4D553943354B2F514F

BEGIN:VCARD
VERSION:2.1
N:NAME_2;
TEL;WORK:039-999-9999
EMAIL;HOME:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f386839792879697b39b9c9e96dd9581" rel="noreferrer noopener nofollow">[email protected]</a>
URL:www.home_updated.fr
END:VCARD

共享联系人一+二:

content://com.android.contacts/contacts/as_multi_vcard/2345r1817-373F294D4F4329413151472F294F312F%3A2345r1820-4D553943354B2F514F4D553943354B2F514F

BEGIN:VCARD
VERSION:2.1
N:NAME_1;
TEL;WORK:039-999-9999
EMAIL;HOME:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9eebeefaffeafbfadef6f1f3fbb0f8ec" rel="noreferrer noopener nofollow">[email protected]</a>
URL:www.home_updated.fr
END:VCARD
BEGIN:VCARD
VERSION:2.1
N:NAME_2;
TEL;WORK:039-999-9999
EMAIL;HOME:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6316130702170607230b0c0e064d0511" rel="noreferrer noopener nofollow">[email protected]</a>
URL:www.home_updated.fr
END:VCARD

正如您在上面所观察到的,在 Uri 上共享两个联系人结果,两个 LOOKUP_KEY 之间用 %3A(ASCII 中的冒号)分隔。我在PeopleActivity.java中找到了这样做的aosp代码了解详情。

因此我可以利用该代码块来利用多个联系人共享:

if (Intent.ACTION_SEND.equals(action) && type != null) {
    Log.d("type", "type: " + type);
    if (type.equals("text/x-vcard")) {
        handleSendContact(intent);
    }
}

void handleSendContact(Intent intent) {
    Uri contactUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    if (contactUri != null) {
        //  getLastPathSegment() decodes "%3A" to ":", so split must be done on colon
        String[] lookupKeys = contactUri.getLastPathSegment().split(":");
        for (String lookupKey : lookupKeys) {
            // Query contact with lookup key
        }
    }
}

关于android - 如何通过intent-filter和action.SEND_MULTIPLE接收多张电子名片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54114037/

相关文章:

只有电话号码的 Android 联系人选择器

android - 在 Android 上获取所有联系人姓名和号码作为字符串列表

android - 在android中读取所有联系人的电话号码

使用workmanager的Android定期位置更新

java - Gson/Volley Android 从响应中获取消息而不是数据

Android发送 Intent 将SD卡中的照片附加为0长度文件

android - 通过 "android.intent.action.SEND" Intent 启动服务

java - Android打开不同apk的代码的应用程序路径是什么?

android: 单击 TableRow 时调用方法

PHP GCM 错误信息 MismatchSenderId