就像应用可以向其他应用发送数据一样,它也可以从其他应用接收数据。想一想用户如何与您的应用互动,以及您希望从其他应用接收哪些类型的数据。例如,社交网络应用可能希望从其他应用接收文本内容(比如有趣的网址)。
其他应用的用户经常通过 Android Sharesheet 或 intent 解析器向您的应用发送数据。向您的应用发送数据的应用必须为相应数据设置 MIME 类型。您的应用可以通过以下方式接收其他应用发送的数据:
- 在清单中有匹配的
intent-filter标记的Activity - 您的应用发布的共享快捷方式。
直接共享目标是深入到您应用中的特定 Activity 的深层链接。它们通常代表一个真实的人或群组,并由 Android Sharesheet 显示。例如,即时通讯应用可以提供某个人的直接共享目标,该目标可以直接深入链接到与此人的对话。如需查看详细说明,请参阅提供直接共享目标。
支持 MIME 类型
理想情况下,应用必须能够接收尽可能广泛的 MIME 类型。
例如,一款旨在发送文本、图片和视频的即时通讯应用最好支持接收 text/*、image/* 和 video/*。以下是在 Android 中发送和接收简单数据时常见的一些 MIME 类型。
| 接收器注册 | 发件人发送 |
|---|---|
text/* |
|
`image/*` |
|
video/* |
|
| 支持的文件扩展名 | application/pdf |
请参阅 MIME 媒体类型的 IANA 官方注册表。
制作出色的共享目标
当用户点按与特定 activity 关联的分享目标时,他们应该能够在使用分享内容之前对内容进行确认和编辑。这对于文本数据尤为重要。
通过 activity 接收数据
通过 activity 接收数据涉及更新清单、处理传入的内容,以及确保用户识别您的应用。
更新您的清单
intent 过滤器会告知系统应用组件接受哪些 intent。您在将简单的数据发送到其他应用一课中学习了如何通过 ACTION_SEND 操作构建 intent,与此类似,您也可以创建 intent 过滤器,以便能够通过此操作接收 intent。您可以使用 <intent-filter> 元素在清单中定义 intent 过滤器。例如,如果您的应用能够处理文本内容接收,那么包含一个或多个任意类型图片的清单将如下面的代码段所示:
<activity android:name=".ui.MyActivity" > <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND_MULTIPLE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter> </activity>
当另一个应用尝试通过构建 intent 并将它传递给 startActivity() 来共享任何这些内容时,您的应用都将在 Android Sharesheet 或 intent 解析器中列为一个选项。如果用户选择您的应用,系统会启动相应的 activity(上例中的 .ui.MyActivity)。然后,您需要在自己的代码和界面中妥当处理相关内容。
处理传入的内容
如需处理 Intent 传送的内容,请调用 getIntent() 来获取 Intent 对象。获得该对象后,您就可以检查其内容以确定后续操作。如果此 activity 可从系统的其他部分(例如启动器)启动,您在检查 intent 时需要将这一点考虑在内。
要格外注意检查传入的数据,因为您永远不知道其他应用可能会向您发送什么。例如,可能设置了错误的 MIME 类型,或者所发送的图像可能非常大。另外,请记住,一定要在主(“界面”)线程以外的独立线程中处理二进制数据。
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
...
when {
intent?.action == Intent.ACTION_SEND -> {
if ("text/plain" == intent.type) {
handleSendText(intent) // Handle text being sent
} else if (intent.type?.startsWith("image/") == true) {
handleSendImage(intent) // Handle single image being sent
}
}
intent?.action == Intent.ACTION_SEND_MULTIPLE
&& intent.type?.startsWith("image/") == true -> {
handleSendMultipleImages(intent) // Handle multiple images being sent
}
else -> {
// Handle other intents, such as being started from the home screen
}
}
...
}
private fun handleSendText(intent: Intent) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
// Update UI to reflect text being shared
}
}
private fun handleSendImage(intent: Intent) {
(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
// Update UI to reflect image being shared
}
}
private fun handleSendMultipleImages(intent: Intent) {
intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let {
// Update UI to reflect multiple images being shared
}
}
Java
void onCreate (Bundle savedInstanceState) {
...
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent); // Handle multiple images being sent
}
} else {
// Handle other intents, such as being started from the home screen
}
...
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
}
}
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// Update UI to reflect image being shared
}
}
void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// Update UI to reflect multiple images being shared
}
}
收到数据之后更新界面的操作可能很简单(比如只需填充 EditText),也可能比较复杂(比如需要将有趣的照片滤镜应用到图片)。接下来执行什么操作取决于您的应用。
分享屏幕截图网址
在截取屏幕截图时,您可以分享该屏幕截图和任何关联的网址。
这样可以提供更丰富的用户体验。接收网址时,请务必从 intent 中获取 EXTRA_TEXT 字段,如以下示例所示:
override fun onCreate(savedInstanceState: Bundle?) {
...
when {
intent?.action == Intent.ACTION_SEND -> {
if (intent.type?.startsWith("image/") == true) {
handleSendImage(intent)
}
}
...
}
private fun handleSendImage(intent: Intent) {
(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
// Handle the EXTRA_TEXT as well
intent.getCharSequenceExtra(Intent.EXTRA_TEXT)
// Update UI to reflect image being shared and the EXTRA_TEXT
// if available
}
}
...
}
确保用户能够识别您的应用
在 Android Sharesheet 和 intent 解析器中,您的应用由其图标和标签表示。两者都在清单中定义。您可以设置 activity 或 intent 过滤器标签来提供更多上下文。
从 Android 10(API 级别 29)开始,Android Sharesheet 将仅使用清单中的 application 标签上设置的图标。Android 会忽略在 intent-filter 和 activity 标记上设置的图标。