提供直接共享目标

图 1:Sharesheet 中的“直接分享”行,如 1 所示

使用“直接分享”目标,让其他应用的用户可以更轻松、更快速地与您的应用分享网址、图片或其他类型的数据。“直接分享”的工作原理是直接在 Android Sharesheet 上显示即时通讯和社交应用中的联系人,而无需用户选择应用,然后搜索联系人。

ShortcutManagerCompat 是一个 AndroidX API,可提供分享快捷方式,并向后兼容已废弃的 ChooserTargetService API。这是发布分享快捷方式和 ChooserTargets 的首选方式。如需查看相关说明,请参阅本页上的使用 AndroidX 同时提供分享快捷方式和 ChooserTarget。

发布直接共享目标

Sharesheet 的“直接分享”行仅显示由 Sharing Shortcuts API 提供的动态快捷方式。请完成以下步骤,发布 Direct Share 目标。

  1. 在应用的 XML 资源文件中,声明 share-target 元素。

    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <share-target android:targetClass="com.example.android.sharingshortcuts.SendMessageActivity">
        <data android:mimeType="text/plain" />
        <category android:name="com.example.android.sharingshortcuts.category.TEXT_SHARE_TARGET" />
    </share-target>
    </shortcuts>
    
  2. 应用初始化时,使用 setDynamicShortcuts 按重要性对动态快捷方式进行排序。

    索引越小,重要性越高。如果您正在开发一款通讯应用,则可以按照在应用中显示的顺序,将最近的对话作为快捷方式。请勿发布过时的快捷方式;如果某个对话在过去 30 天内没有任何用户活动,则会被视为过时。

    Kotlin

    ShortcutManagerCompat.setDynamicShortcuts(myContext, listOf(shortcut1, shortcut2, ..))

    Java

    List<ShortcutInfoCompat> shortcuts = new ArrayList<>();
    shortcuts.add(shortcut1);
    shortcuts.add(shortcut2);
    ...
    ShortcutManagerCompat.setDynamicShortcuts(myContext, shortcuts);
  3. 如果您正在开发一款通信应用,请在用户每次收到或向联系人发送消息时,立即通过 pushDynamicShortcut 报告快捷方式使用情况。如需了解详情,请参阅本页中的报告通信应用的快捷方式使用情况。例如,通过 ShortcutInfoCompat.Builder#addCapabilityBinding 为快捷方式指定 actions.intent.SEND_MESSAGE 功能绑定,以报告用户发送的消息的使用情况。

    Kotlin

    val shortcutInfo = ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
      ...
      .setShortLabel(firstName)
      .setLongLabel(fullName)
      .setCategories(matchedCategories)
      .setLongLived(true)
    .addCapabilityBinding("actions.intent.SEND_MESSAGE").build()
    ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo)

    Java

    ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
      ...
      .setShortLabel(firstName)
      .setLongLabel(fullName)
      .setCategories(matchedCategories)
      .setLongLived(true)
      .addCapabilityBinding("actions.intent.SEND_MESSAGE")
      .build();
    
    ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo);
  4. 如果用户删除了联系人,请使用 removeLongLivedShortcut。无论快捷方式是否由系统服务缓存,这都是移除快捷方式的首选方式。以下代码段展示了如何执行此操作。

    Kotlin

    val deleteShortcutId = "..."
    ShortcutManagerCompat.removeLongLivedShortcuts(myContext, listOf(deleteShortcutId))

    Java

    String deleteShortcutId = "...";
    ShortcutManagerCompat.removeLongLivedShortcuts(
        myContext, Arrays.asList(deleteShortcutId));

提高直接共享目标的排名

Android Sharesheet 显示固定数量的直接共享目标,这些建议按排名排序。您可以通过执行以下操作来提升快捷方式的排名:

  • 确保所有 shortcutIds 都是唯一的,并且绝不会针对不同的目标重复使用。
  • 通过调用 setLongLived(true) 确保快捷方式是长效快捷方式。
  • 对于与对话相关的快捷方式,请通过 ShortcutManagerCompat.pushDynamicShortcut 重新发布相应的快捷方式,以报告外发和收到的消息的快捷方式使用情况。如需了解详情,请参阅本页中的报告通信应用的快捷方式使用情况。
  • 避免提供不相关或过时的“直接分享”目标,例如用户在过去 30 天内未发送过消息的联系人。
  • 对于短信应用,请避免为短号码或被识别为潜在垃圾内容的对话提供快捷方式。用户极不可能分享到这些对话。
  • 调用 setCategories(),将快捷方式与适当的 mimeType 属性相关联。例如,对于短信应用,如果联系人未启用 RCS 或 MMS,您就不会将相应的快捷方式与非文本 MIME 类型(例如 image/* 和 video/*)相关联。
  • 对于给定的对话,一旦推送了动态快捷方式并报告了使用情况,请勿更改快捷方式 ID。这样可以确保保留使用情况数据以进行排名。

如果用户点按任何“直接分享”目标,您的应用必须将他们转到可直接对目标主题执行操作的界面。请勿向用户显示消除歧义界面,也不要将用户置于与点按目标无关的界面中。例如,在即时通讯应用中,点按“直接分享”目标会将用户转到与所选用户的会话视图。键盘可见,并且消息中预填充了共享数据。

Sharing Shortcuts API

从 Android 10(API 级别 29)开始,ShortcutInfo.Builder 添加了方法和增强功能,可提供有关分享目标的更多信息:

setCategories()
从 Android 10 开始,类别还用于过滤可处理分享 intent 或操作的快捷方式。如需了解详情,请参阅声明分享目标。对于要用作共享目标的快捷方式,此字段是必需的。
setLongLived()

指定快捷方式在被应用取消发布或设为不可见(作为动态或固定快捷方式)时是否有效。如果快捷方式是长效快捷方式,则即使它已作为动态快捷方式取消发布,也可以由各种系统服务进行缓存。

使快捷方式长期存在有助于提高其排名。如需了解详情,请参阅获得最佳排名。

setShortLabel()、setLongLabel()

发布指向个人的快捷方式时,请在 setLongLabel() 中添加其全名,并在 setShortLabel() 中添加任何简称(例如昵称或名字)。

查看在 GitHub 上发布分享快捷方式的示例。

提供快捷方式图像

如需创建分享快捷方式,你需要通过 setIcon() 添加图片。

分享快捷方式可能会出现在系统界面中,并且可能会被重新调整。 此外,某些搭载 Android 7、8 或 9(API 级别 25、26、27 和 28)的设备可能会显示没有背景的纯位图图标,这会显著降低对比度。为确保快捷方式按预期显示,请使用 IconCompat.createWithAdaptiveBitmap() 提供自适应位图。

确保自适应位图遵循为自适应图标设置的相同准则和尺寸。 最常见的方法是将目标方形位图缩放到 72x72 dp,并将其置于 108x108 dp 透明画布的中心。如果您的图标包含透明区域,则需要添加背景颜色;否则,透明区域会显示为黑色。

不要提供框在特定形状中的图像。例如,在 Android 10(API 级别 29)之前,通常会为直接分享 ChooserTarget 提供遮盖成圆圈的用户头像。Android 10 中的 Android Sharesheet 和其他系统界面现在可以设置快捷方式图像的形状和主题。提供分享快捷方式的首选方法是通过 ShortcutManagerCompat 自动将向后兼容的直接分享 ChooserTarget 对象调整为圆形。

声明共享目标

分享目标必须在应用的资源文件中声明,类似于静态快捷方式定义。在资源文件的 <shortcuts> 根元素内添加分享目标定义,以及其他静态快捷方式定义。每个 <share-targets> 元素都包含有关共享数据类型、匹配类别以及将处理共享 intent 的目标类的信息。XML 代码如下所示:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <share-target android:targetClass="com.example.android.sharingshortcuts.SendMessageActivity">
    <data android:mimeType="text/plain" />
    <category android:name="com.example.android.sharingshortcuts.category.TEXT_SHARE_TARGET" />
  </share-target>
</shortcuts>

共享目标中的数据元素类似于 intent 过滤器中的数据规范。每个分享目标都可以有多个类别,这些类别仅用于将应用发布的快捷方式与其分享目标定义进行匹配。类别可以具有应用定义的任意值。

如果用户在 Android Sharesheet 中选择与上述示例 target-share 匹配的分享快捷方式,应用将收到以下分享 intent:

Action: Intent.ACTION_SEND
ComponentName: {com.example.android.sharingshortcuts /
                com.example.android.sharingshortcuts.SendMessageActivity}
Data: Uri to the shared content
EXTRA_SHORTCUT_ID: <ID of the selected shortcut>

如果用户从启动器快捷方式打开分享目标,应用将获取在向 ShortcutManagerCompat 添加分享快捷方式时创建的 intent。由于这是一个不同的 intent,因此 Intent.EXTRA_SHORTCUT_ID 将不可用,如果您需要该 ID,则必须手动传递。

报告通信应用的快捷方式使用情况

如果您正在开发一款通讯应用,则可以通过报告外发消息和收到的消息的使用情况来提高在 Android Sharesheet 中的排名。为此,请通过 ShortcutManagerCompat.pushDynamicShortcut 重新发布代表联系人的对话快捷方式。

快捷方式使用情况和功能绑定向后兼容 Android 5.0(API 21)。

报告外发消息的快捷方式使用情况

报告用户发送的消息的使用情况,在功能上类似于在创建消息后点击“发送”按钮。

如需触发用量报告,请通过 ShortcutInfoCompat.Builder#addCapabilityBinding 在快捷方式中指定具有 actions.intent.SEND_MESSAGE 功能的功能绑定。

Kotlin

val shortcutInfo = ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
  ...
  .setShortLabel(firstName)
  .setLongLabel(fullName)
  .setCategories(matchedCategories)
  .setLongLived(true)
.addCapabilityBinding("actions.intent.SEND_MESSAGE").build()
ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo)

Java

ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
  ...
  .setShortLabel(firstName)
  .setLongLabel(fullName)
  .setCategories(matchedCategories)
  .setLongLived(true)
  .addCapabilityBinding("actions.intent.SEND_MESSAGE")
  .build();

ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo);

如果外发的消息是针对群聊,您还必须添加 Audience 参数值,因为 recipient 类型与该 capability 相关联。

Kotlin

val shortcutInfo = ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
  ...
  .setShortLabel(groupShortTitle)
  .setLongLabel(groupLongTitle)
  .setCategories(matchedCategories)
  .setLongLived(true)
  .addCapabilityBinding("actions.intent.SEND_MESSAGE", "message.recipient.@type", listOf("Audience")).build()

ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo)

Java

ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
  ...
  .setShortLabel(groupShortTitle)
  .setLongLabel(groupLongTitle)
  .setCategories(matchedCategories)
  .setLongLived(true)
  .addCapabilityBinding("actions.intent.SEND_MESSAGE", "message.recipient.@type", Arrays.asList("Audience"))
  .build();

ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo);

报告收到的消息的快捷方式使用情况

如需在用户收到短信、聊天消息、电子邮件或通知等消息时触发用量报告,您还必须通过 ShortcutInfoCompat.Builder#addCapabilityBinding 在快捷方式中指定功能绑定,并提供 actions.intent.RECEIVE_MESSAGE 功能。

Kotlin

val shortcutInfo = ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
  ...
  .setShortLabel(firstName)
  .setLongLabel(fullName)
  .setCategories(matchedCategories)
  .setLongLived(true)
  .addCapabilityBinding("actions.intent.RECEIVE_MESSAGE").build()

ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo)

Java

ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
  ...
  .setShortLabel(firstName)
  .setLongLabel(fullName)
  .setCategories(matchedCategories)
  .setLongLived(true)
  .addCapabilityBinding("actions.intent.RECEIVE_MESSAGE")
  .build();

ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo);

如果收到的消息来自群聊,您还必须添加 Audience 参数值,因为 sender 类型与该 capability 相关联。

Kotlin

val shortcutInfo = ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
  ...
  .setShortLabel(groupShortTitle)
  .setLongLabel(groupLongTitle)
  .setCategories(matchedCategories)
  .setLongLived(true)
  .addCapabilityBinding("actions.intent.RECEIVE_MESSAGE", "message.sender.@type", listOf("Audience")).build()

ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo)

Java

ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(myContext, staticConversationIdentifier)
  ...
  .setShortLabel(groupShortTitle)
  .setLongLabel(groupLongTitle)
  .setCategories(matchedCategories)
  .setLongLived(true)
  .addCapabilityBinding("actions.intent.RECEIVE_MESSAGE", "message.sender.@type", Arrays.asList("Audience"))
  .build();

ShortcutManagerCompat.pushDynamicShortcut(myContext, shortcutInfo);

使用 AndroidX 同时提供分享快捷方式和 ChooserTarget

为了能够使用 AndroidX 兼容性库,应用的清单必须包含 meta-data chooser-target-service 和 intent-filters 集。请参阅当前的 ChooserTargetService Direct Share API。

此服务已在兼容性库中声明,因此用户无需在应用的清单中声明该服务。不过,必须将从分享 activity 到服务的链接视为选择器目标提供程序。

在以下示例中,ChooserTargetService 的实现为 androidx.core.content.pm.ChooserTargetServiceCompat,已在 AndroidX 中定义:

<activity
    android:name=".SendMessageActivity"
    android:label="@string/app_name"
    android:theme="@style/SharingShortcutsDialogTheme">
    <!-- This activity can respond to Intents of type SEND -->
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <!-- Only needed if you import the sharetarget AndroidX library that
         provides backwards compatibility with the old DirectShare API.
         The activity that receives the Sharing Shortcut intent needs to be
         taken into account with this chooser target provider. -->
    <meta-data
        android:name="android.service.chooser.chooser_target_service"
        android:value="androidx.sharetarget.ChooserTargetServiceCompat" />
</activity>

共享快捷方式常见问题解答

快捷方式使用情况数据如何存储?这些数据会离开设备吗?

快捷方式完全存储在设备上加密磁盘分区的系统数据目录中。快捷方式中的信息(例如图标、intent 以及人员和资源的名称)只能由系统服务和发布快捷方式的同一应用访问。

“直接分享”的历史是怎样的?

我们在 Android 6.0(API 级别 23)中引入了 Direct Share,以便应用通过 ChooserTargetService 提供 ChooserTarget 对象。结果是根据需求被动检索的,导致目标加载时间较慢。

在 Android 10(API 级别 29)中,我们用新的 Sharing Shortcuts API 取代了 ChooserTargetService Direct Share API。分享快捷方式 API 允许应用提前发布直接分享目标,而不是按需被动检索结果。这大大加快了在准备 ShareSheet 时检索 Direct Share 目标的过程。ChooserTargetService 直接分享机制将继续有效,但系统会将通过这种方式提供的目标排在任何使用 Sharing Shortcuts API 的目标之后。

Android 11(API 级别 30)弃用了 ChooserTargetService 服务,Sharing Shortcuts API 是提供直接分享目标的唯一方法。

为分享目标发布的快捷方式与启动器快捷方式(在启动器中长按应用图标时快捷方式的典型用法)有何不同?

为“分享目标”目的发布的任何快捷方式也是启动器快捷方式,会在长按应用图标时显示在菜单中。每个 activity 的快捷方式数量上限也适用于应用发布的快捷方式总数(分享目标和旧版启动器快捷方式的总和)。

关于应发布的分享快捷方式数量,有什么指导建议?

分享快捷方式的数量上限与通过 getMaxShortcutCountPerActivity(android.content.Context) 提供的动态快捷方式的数量上限相同。您可以发布任意数量的快捷方式,但必须注意,分享快捷方式可能会在应用启动器长按菜单和分享表中显示。大多数应用启动器在长按时,在竖屏模式下最多显示 4 或 5 个快捷方式,在横屏模式下最多显示 8 个快捷方式。如需详细了解如何分享快捷方式,请参阅此常见问题解答。