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

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

其他應用程式的使用者經常透過 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)。那麼您可以自行在程式碼和 UI 中妥善處理內容。

處理傳入的內容

如要處理 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 Sharesheet 和意圖解析器中,您的應用程式會以圖示標籤表示。在資訊清單中定義兩者。您可以設定活動或意圖篩選器標籤來提供更多背景資訊。

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