ShareIntent API
通过 Share Intent 在您的 WebView 应用中直接接收从其他 Android 应用分享的内容。
WebViewGold 的 ShareIntent API 让您的应用能够作为目标出现在 Android 的原生分享对话框中。这样,用户就可以将文本、URL 或图片(以 base64 字符串形式)等内容从任何其他应用直接分享到您基于 WebView 的应用中。
使用场景:
- 将指向用户个人资料、笔记或文章的链接直接分享到您的应用中
- 接收来自消息或浏览器应用的内容
- 将图库中的图片发送到您的 WebView 界面(base64 编码)
步骤 1:在 AndroidManifest.xml 中启用分享支持
若要让您的应用能够接收分享的数据,请取消您 AndroidManifest.xml 文件中相关 intent 过滤器的注释:
<!-- Receive shared text (e.g., URLs or notes) -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<!-- Receive a single shared image -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<!-- Receive multiple shared images -->
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
这些过滤器生效后,每当用户从其他应用分享内容时,您基于 WebViewGold 的应用图标就会作为分享目标出现。
步骤 2:在 WebView 中处理分享的数据
当用户向您的应用分享内容时,WebViewGold 会自动将分享的内容注入到名为 sharedData 的 JavaScript 变量中。
<script>
alert(sharedData); // Displays the shared content (text or base64 image)
</script>
- 文本或 URL: 将作为纯文本传入
sharedData - 图片: 将被转换为 base64 编码的字符串并传入
sharedData
使用示例: 根据接收到的内容自动填充表单或预览图片:
<script>
if (sharedData) {
document.getElementById("myInput").value = sharedData;
}
</script>
注意: 请确保您的 JavaScript 已准备好同时处理纯文本和较长的 base64 字符串。您也可以实现客户端检查,以区分不同的内容类型。