提供直接分享目標

圖 1:Sharesheet 中的「直接分享」列,如 1 所示

使用 Direct Share 目標,可讓其他應用程式的使用者更輕鬆快速地與您的應用程式分享網址、圖片或其他類型的資料。Direct Share 的運作方式是直接在 Android Sharesheet 上顯示訊息和社交應用程式中的聯絡人,使用者不必選取應用程式,然後搜尋聯絡人。

ShortcutManagerCompat 是提供分享捷徑的 AndroidX API,可回溯相容於已淘汰的 ChooserTargetService API。這是發布分享捷徑和 ChooserTargets 的首選方式。如需操作說明,請參閱本頁面的「使用 AndroidX 同時提供分享捷徑和 ChooserTargets」。

發布直接分享目標

Sharesheet 的「直接分享」列只會顯示 Sharing Shortcuts API 提供的動態捷徑。請完成下列步驟,發布直接分享目標。

  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。這可確保保留使用資料,用於排名。

如果使用者輕觸任何「直接分享」目標,應用程式必須將他們導向 UI,讓他們可以直接對目標主體執行動作。請勿向使用者顯示消歧 UI,也不要將他們導向與輕觸目標無關的 UI。舉例來說,在訊息應用程式中,輕觸「直接分享」目標會將使用者導向與所選對象的會話群組檢視畫面。鍵盤會顯示,且訊息會預先填入共用資料。

Sharing Shortcuts API

自 Android 10 (API 級別 29) 起,ShortcutInfo.Builder 新增了方法和強化功能,可提供分享目標的其他資訊:

setCategories()
從 Android 10 開始,類別也可用於篩選可處理分享意圖或動作的捷徑。詳情請參閱「宣告共用目標」。如果捷徑要用做分享目標,則必須填寫這個欄位。
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> 元素都包含共用資料類型、相符類別,以及處理分享意圖的目標類別等資訊。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>

共用目標中的資料元素與意圖篩選器中的資料規格類似。每個分享目標都可以有多個類別,這些類別僅用於比對應用程式發布的捷徑與其分享目標定義。類別可以有任何應用程式定義的值。

如果使用者在 Android Sharesheet 中選取與上述範例 target-share 相符的分享捷徑,應用程式會收到下列分享意圖:

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.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 類型與該功能相關聯。

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 類型與該功能相關聯。

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 同時提供分享捷徑和 ChooserTargets

如要使用 AndroidX 相容性程式庫,應用程式資訊清單必須包含 meta-data chooser-target-service 和意圖篩選器組合。請參閱目前的 ChooserTargetService Direct Share API。

這項服務已在相容性程式庫中宣告,因此使用者不需要在應用程式資訊清單中宣告服務。不過,從分享活動到服務的連結,必須視為選擇器目標供應器。

在以下範例中,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>

分享捷徑常見問題

快速鍵使用資料如何儲存?會傳出裝置嗎?

捷徑會完全儲存在裝置端,位於加密磁碟分區的系統資料目錄中。捷徑中的資訊 (例如圖示、意圖以及使用者和資源的名稱) 只能由系統服務和發布捷徑的相同應用程式存取。

「直接分享」的歷史為何?

我們在 Android 6.0 (API 級別 23) 中推出直接分享功能,讓應用程式透過 ChooserTargetService 提供 ChooserTarget 物件。系統會根據需求被動擷取結果,導致目標載入時間緩慢。

在 Android 10 (API 級別 29) 中,我們將 ChooserTargetService Direct Share API 替換為新的 Sharing Shortcuts API。Sharing Shortcuts API 可讓應用程式預先發布直接分享目標,而不是在需要時才被動擷取結果。這可大幅加快準備 ShareSheet 時擷取「直接分享」目標的速度。ChooserTargetService Direct Share 機制會繼續運作,但系統會將以此方式提供的目標排名,排在任何使用 Sharing Shortcuts API 的目標之後。

Android 11 (API 級別 30) 已淘汰 ChooserTargetService 服務,而 Sharing Shortcuts API 是提供 Direct Share 目標的唯一方式。

為分享目標發布的捷徑,與啟動器捷徑 (在啟動器中長按應用程式圖示時,捷徑的一般用法) 有何不同?

任何以「分享目標」為目的發布的捷徑,也都是啟動器捷徑,並會在長按應用程式圖示時顯示在選單中。每個活動的捷徑數量上限,也適用於應用程式發布的捷徑總數 (分享目標和舊版啟動器捷徑的總和)。

發布分享捷徑的數量建議

分享捷徑的數量限制與透過 getMaxShortcutCountPerActivity(android.content.Context) 提供的動態捷徑數量限制相同。您可以發布任意數量的捷徑,但請注意,分享捷徑可能會顯示在應用程式啟動器長按和分享工作表中。在直向模式下,大多數應用程式啟動器長按時最多會顯示四或五個捷徑,橫向模式下則會顯示八個。如需更多詳細資訊和共用捷徑的指南,請參閱常見問題。