應用程式可以將資料傳送給其他應用程式,也可以接收其他應用程式傳送的資料。請思考使用者與應用程式的互動方式,以及您想從其他應用程式接收哪些資料類型。舉例來說,社群網路應用程式可能會想接收來自其他應用程式的文字內容,例如有趣的網頁網址。
在使用 Compose Navigation 的應用程式中,接收其他應用程式傳送的資料分為兩個步驟:
在資訊清單中宣告
<intent-filter>:您仍須在 AndroidManifest.xml 檔案中,將意圖篩選器新增至 Activity。這會告知 Android 作業系統,您的應用程式可以接收特定動作 (例如 ACTION_SEND) 和 MIME 類型 (例如 text/plain)。在 Compose 導覽圖中定義 navDeepLink:不必在 Activity 的 onCreate 或 onNewIntent 方法中手動攔截及剖析 Intent,而是使用 Compose 導覽的深層連結支援功能。將符合動作和 MIME 類型的 navDeepLink 直接附加至應顯示內容的可組合目的地。NavController 會自動攔截 Intent,並直接將使用者導向該畫面。
直接分享目標是應用程式中特定 Activity 的深層連結, 通常代表個人或群組,並顯示在 Android Sharesheet 中。舉例來說,訊息應用程式可以為使用者提供「直接分享」目標,直接深層連結至與該使用者的對話。如需詳細操作說明,請參閱「提供直接分享目標」。
支援的 MIME 類型
理想情況下,應用程式應能接收最廣泛的 MIME 類型。舉例來說,如果訊息應用程式的設計用途是傳送文字、圖片和影片,最好支援接收 text/*、image/* 和 video/*。以下列出幾個常見的 MIME 類型,方便您在 Android 中傳送及接收簡單資料。
| 接收器註冊 | 傳送者傳送 |
|---|---|
text/* |
|
image/* |
|
video/* |
|
| 支援的副檔名 | application/pdf |
請參閱 IANA 官方 MIME 媒體類型登錄資料庫。
製作優質的分享目標
使用者輕觸與特定活動相關聯的分享目標時,應該能夠先確認及編輯分享內容,再使用該內容。這對文字資料來說尤其重要。
透過活動接收資料
如要接收活動資料,請更新資訊清單、處理傳入的內容,並確保使用者能辨識您的應用程式。
更新資訊清單
意圖篩選器會告知系統應用程式元件接受哪些意圖。與您在「傳送簡單資料給其他應用程式」課程中建構含 ACTION_SEND 動作的意圖類似,您會建立意圖篩選器來接收含此動作的意圖。您可以使用 <intent-filter> 元素,在資訊清單中定義意圖篩選器。舉例來說,如果應用程式會接收文字內容,包含一或多張任何類型的圖片的資訊清單,看起來會像下列程式碼片段:
<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>
當其他應用程式嘗試建構意圖並傳遞至 startActivity(),藉此分享上述任何內容時,您的應用程式會列為 Android Sharesheet 或意圖解析器中的選項。如果使用者選取您的應用程式,系統就會啟動對應的活動 (上例中的 .ui.MyActivity)。接著,您可以在程式碼和 UI 中適當處理內容。
處理傳入的內容
如要處理 Intent 傳送的內容,請呼叫 getIntent() 取得 Intent 物件。取得物件後,即可檢查內容,判斷後續動作。如果可從系統其他部分 (例如啟動器) 啟動這項活動,請在檢查意圖時將這點納入考量。
請特別留意檢查傳入的資料,您永遠不知道其他應用程式可能會傳送什麼內容。舉例來說,可能是設定了錯誤的 MIME 類型,或是傳送的圖片過大。此外,請記得在獨立執行緒中處理二進位資料,而非主 (「UI」) 執行緒。
@Composable fun SharesheetHandler() { val context = LocalContext.current val intent = (context as? Activity)?.intent when (intent?.action) { 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_SEND_MULTIPLE -> { if (intent.type?.startsWith("image/") == true) { handleSendMultipleImages(intent) // Handle multiple images being sent } } else -> { // Handle other intents, such as being started from the home screen } } } fun handleSendText(intent: Intent) { intent.getStringExtra(Intent.EXTRA_TEXT)?.let { // Update ViewModel state to change state of text being shared } } fun handleSendImage(intent: Intent) { IntentCompat.getParcelableExtra(intent, Intent.EXTRA_STREAM, Uri::class.java).let { // Update ViewModel state to change state of image being shared } } fun handleSendMultipleImages(intent: Intent) { IntentCompat.getParcelableArrayListExtra(intent, Intent.EXTRA_STREAM, Uri::class.java).let { // Update ViewModel state to change state of image(s) being shared } }
收到資料後更新 UI 可以很簡單,例如填入 TextField,也可以很複雜,例如對圖片套用有趣的相片濾鏡。後續動作由應用程式決定。
分享螢幕截圖網址
擷取螢幕截圖時,你可以分享螢幕截圖和任何相關聯的網址。
提供更豐富的使用者體驗。收到網址時,請務必從 Intent 取得 EXTRA_TEXT 欄位,如下列範例所示:
IntentCompat.getParcelableExtra(intent, Intent.EXTRA_STREAM, Uri::class.java).let { // Handle the EXTRA_TEXT as well val extraText = intent.getCharSequenceExtra(Intent.EXTRA_TEXT) // Update ViewModel state to change state image being shared and the EXTRA_TEXT // if available }
確保使用者能認出您的應用程式
在 Android 分享功能表和意圖解析器中,應用程式會以圖示和標籤表示。這兩者都是在資訊清單中定義。您可以設定活動或意圖篩選器標籤,提供更多背景資訊。
自 Android 10 (API 級別 29) 起,Android Sharesheet 只會使用 application 標記中資訊清單設定的圖示。Android 會忽略在 intent-filter 和 activity 標記中設定的圖示。