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

應用程式可以傳送資料給其他應用程式,也可以接收其他應用程式傳送的資料。請思考使用者與應用程式的互動方式,以及您想從其他應用程式接收的資料類型。舉例來說,社群網路應用程式可能想接收來自其他應用程式的文字內容,例如有趣的網頁網址。

其他應用程式的使用者經常透過 Android 分享功能表或意圖解析器,將資料傳送至您的應用程式。將資料傳送至您應用程式的應用程式,必須為該資料設定 MIME 類型。您的應用程式可以透過下列方式接收其他應用程式傳送的資料:

  • 資訊清單中具有相符 intent-filter 標記的 Activity
  • 分享應用程式發布的捷徑。

直接分享目標是應用程式中特定活動的深層連結, 通常代表個人或群組,並顯示在 Android 分享功能表中。 舉例來說,訊息應用程式可以為使用者提供「直接分享」目標,直接深層連結至與該使用者的對話。如需詳細操作說明,請參閱「提供直接分享目標」一文。

支援的 MIME 類型

理想情況下,應用程式應能接收範圍最廣的 MIME 類型。舉例來說,如果訊息應用程式的設計用途是傳送文字、圖片和影片,最好支援接收 text/*image/*video/*。以下列出幾個常見的 MIME 類型,可用於在 Android 中傳送及接收簡單資料。

接收器註冊 寄件者傳送
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 分享功能表或意圖解析器中的選項。如果使用者選取您的應用程式,系統就會啟動對應的活動 (上例中的 .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 分享功能表和意圖解析器中,應用程式會以圖示標籤表示。這兩者都是在資訊清單中定義。您可以設定活動或意圖篩選器標籤,提供更多背景資訊。

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