天天看點

【Android Training - 06】分享資料内容 [Lesson 2 - 從其它app接收分享的内容] Receiving Content from Other Apps[從其他app接收分享的内容]

Receiving Content from Other Apps[從其他app接收分享的内容]

  • 就像你的程式能夠發送資料到其他程式一樣,其他程式也能夠簡單的接收發送過來的資料。需要考慮的是使用者與你的程式如何進行互動,你想要從其他程式接收哪些資料類型。例如,一個社交網絡程式會希望能夠從其他程式接受文本資料,像一個有趣的網址連結。Google+的Android用戶端會接受文本資料與單張或者多張圖檔。用這個app,使用者可以簡單的從Gallery程式選擇一張圖檔來啟動Google+進行釋出。

Update Your Manifest[更新你的manifest檔案]

  • Intent filters通知了Android系統說,一個程式會接受哪些資料。像上一課一樣,你可以建立intent filters來表明程式能夠接收哪些action。下面是個例子,對三個activit分别指定接受單張圖檔,文本與多張圖檔。【這裡有不清楚Intent filter的,請參考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>
           
  • 當另外一個程式嘗試分享一些東西的時候,你的程式會被呈現在一個清單裡面讓使用者進行選擇。如果使用者選擇了你的程式,相應的activity就應該被調用開啟,這個時候就是你如何處理擷取到的資料的問題了。

Handle the Incoming Content[處理接受到的資料]

  • 為了處理從Intent帶過來的資料,可以通過調用getIntent()方法來擷取到Intent對象。一旦你拿到這個對象,你可以對裡面的資料進行判斷,進而決定下一步應該做什麼。請記住,如果一個activity可以被其他的程式啟動,你需要在檢查intent的時候考慮這種情況(是被其他程式而調用啟動的)。
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線程裡面去處理那些擷取到的資料。
  • 更新UI可以像更新EditText一樣簡單,也可以是更加複雜一點的操作,例如過濾出感興趣的圖檔。It's really specific to your application what happens next.

學習自: http://developer.android.com/training/sharing/receive.html,請多指教,謝謝! 轉載請注明出處:http://blog.csdn.net/kesenhoo,謝謝!