接收其他應用程式傳送的簡單資料

就像應用程式可傳送資料至其他應用程式一樣,也能接收其他應用程式的資料 應用程式思考使用者與應用程式的互動方式,以及哪些資料 您希望從其他應用程式接收的類型。例如, 網路應用程式可能有興趣接收文字內容,例如 產生有趣的網址

其他應用程式的使用者經常透過 Android 傳送資料至您的應用程式 Sharesheet 或意圖解析器。傳送資料至應用程式的應用程式必須設定 該資料的 MIME 類型。您的應用程式可在以下位置接收其他應用程式傳送的資料: 方法如下:

  • 資訊清單中有相符的 intent-filter 標記的 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

請參閱 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 上述範例)。就必須妥善處理內容 程式碼和使用者介面的變化

處理傳入的內容

如要處理 Intent 傳送的內容,請呼叫 getIntent() 以取得 Intent 物件。取得物件後 就能檢查其中內容,決定後續行動。如果這項活動可以 是從系統的其他部分 (例如啟動器) 啟動, 都會將模型納入考量

仔細檢查傳入的資料,你永遠不知道 應用程式可能會傳送舉例來說,可能設定的 MIME 類型有誤,或 傳送的圖片可能會非常大另外,請記得處理二進位資料 單獨執行緒,而非主要 (「UI」) 執行緒。

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
    }
}

在收到資料後更新 UI 很簡單,就像填入 EditText,或者 例如為圖像套用有趣的相片濾鏡取決於 如何影響應用程式的後續步驟

確保使用者認得您的應用程式

您的應用程式是以 圖示和 Android 中的 label Sharesheet 和意圖解析器。在資訊清單中定義兩者。你可以 設定活動或意圖篩選器標籤來提供更多背景資訊。

自 Android 10 (API 級別 29) 起,Android Sharesheet 只會使用 定義 application 標記的資訊清單。Android 會忽略在 intent-filteractivity 標記。