Android接收從其他App傳送來的數(shù)據(jù)

2018-08-02 17:29 更新

編寫:kesenhoo - 原文:http://developer.android.com/training/sharing/receive.html

就像我們的程序能夠分享數(shù)據(jù)給其他程序一樣,其也能方便的接收來自其他程序的數(shù)據(jù)。需要考慮的是用戶與我們的程序如何進(jìn)行交互,以及我們想要從其他程序接收數(shù)據(jù)的類型。例如,一個(gè)社交網(wǎng)絡(luò)程序可能會(huì)希望能夠從其他程序接受文本數(shù)據(jù),比如一個(gè)有趣的網(wǎng)址鏈接。Google+的Android客戶端會(huì)接受文本數(shù)據(jù)與單張或者多張圖片。用戶可以簡單的從Gallery程序選擇一張圖片來啟動(dòng)Google+,并利用其發(fā)布文本或圖片。

更新我們的manifest文件(Update Your Manifest)

Intent filters告訴Android系統(tǒng)一個(gè)程序愿意接受的數(shù)據(jù)類型。類似于上一課,我們可以創(chuàng)建intent filters來表明程序能夠接收的action類型。下面是個(gè)例子,對(duì)三個(gè)activit分別指定接受單張圖片,文本與多張圖片。(Intent filter相關(guān)資料,請(qǐng)參考Intents and Intent Filters)

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

當(dāng)某個(gè)程序嘗試通過創(chuàng)建一個(gè)intent并將其傳遞給startActivity來分享一些東西時(shí),我們的程序會(huì)被呈現(xiàn)在一個(gè)列表中讓用戶進(jìn)行選擇。如果用戶選擇了我們的程序,相應(yīng)的activity會(huì)被調(diào)用開啟,這個(gè)時(shí)候就是我們?nèi)绾翁幚慝@取到的數(shù)據(jù)的問題了。

處理接受到的數(shù)據(jù)(Handle the Incoming Content)

為了處理從Intent帶來的數(shù)據(jù),可以通過調(diào)用getIntent()方法來獲取到Intent對(duì)象。拿到這個(gè)對(duì)象后,我們可以對(duì)其中面的數(shù)據(jù)進(jìn)行判斷,從而決定下一步行為。請(qǐng)記住,如果一個(gè)activity可以被其他的程序啟動(dòng),我們需要在檢查intent的時(shí)候考慮這種情況(是被其他程序而調(diào)用啟動(dòng)的)。

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

請(qǐng)注意,由于無法知道其他程序發(fā)送過來的數(shù)據(jù)內(nèi)容是文本還是其他類型的數(shù)據(jù),若數(shù)據(jù)量巨大,則需要大量處理時(shí)間,因此我們應(yīng)避免在UI線程里面去處理那些獲取到的數(shù)據(jù)。

更新UI可以像更新EditText一樣簡單,也可以是更加復(fù)雜一點(diǎn)的操作,例如過濾出感興趣的圖片。這完全取決于我們的應(yīng)用接下來要做些什么。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)