接收来自其他应用的简单数据

就像应用可以向其他应用发送数据一样,它也可以接收来自其他应用的数据 应用。考虑用户如何与您的应用互动,以及哪些数据 您希望从其他应用接收哪些类型的信息。例如,社交媒体 网络应用可能有兴趣接收文本内容,例如 从另一个应用获取有趣的网址

其他应用的用户经常通过 Android Sharesheet 或 intent 解析器。向您的应用发送数据的应用必须设置 相应数据的 MIME 类型。您的应用可以接收 方法:

  • 在清单中有匹配的 intent-filter 标记的 Activity
  • 您的应用发布的共享快捷方式。

直接分享目标是指指向应用内特定 activity 的深层链接。 它们通常代表个人或群体,由 Android Sharesheet 显示。 例如,即时通讯应用可为符合以下条件的用户提供直接共享目标: 直接深层链接到与对方的对话中请参阅 提供直接共享目标,详细了解 操作说明。

支持 MIME 类型

理想情况下,应用必须能够接收尽可能广泛的 MIME 类型。 例如,一款用于发送文字、图片和视频的即时通讯应用 理想情况下支持接收 text/*image/*video/*。以下是一些 用于在 Android 中发送和接收简单数据的常见 MIME 类型。

接收方注册 发件人发送
text/*
  • text/plain
  • text/rtf
  • text/html
  • text/json
`image/*`
  • image/jpg
  • image/png
  • image/gif
video/*
  • video/mp4
  • video/3gp
支持的文件扩展名 application/pdf

请参阅 MIME 媒体类型的 IANA 官方注册表。

创建出色的共享目标

当用户点按与特定活动相关的共享目标时, 应该能够在使用共享内容之前确认和修改这些内容。这是 这对文本数据尤为重要。

使用 activity 接收数据

通过 activity 接收数据涉及更新清单、处理 传入的内容,并确保用户能够识别您的应用。

更新您的清单

intent 过滤器会告知系统应用组件接受哪些 intent。 与您在ACTION_SEND 将简单的数据发送到其他应用 您将创建 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 对象。获得对象后 您可以检查其内容以确定后续操作。如果此活动可以 是从系统的其他部分(例如启动器)启动的,则将此 在检查意图时要考虑的因素。

要特别注意检查传入的数据,因为你永远不知道 应用程序可能向您发送的电子邮件。例如,可能设置了错误的 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,也可以 复杂,比如将有趣的照片滤镜应用到图片。上限为 以及接下来会发生什么

确保用户能够识别您的应用

您的应用由 图标标签 Sharesheet 和 intent 解析器。两者都在清单中定义。您可以 设置 activity 或 intent 过滤器标签以提供更多上下文。

从 Android 10(API 级别 29)开始,Android Sharesheet 仅使用 在 application 标记中添加清单。Android 会忽略 intent-filteractivity 标记。