注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術一般,由于喜愛安卓而產生了翻譯的念頭,純屬個人興趣愛好。
原文鏈接: http://developer.android.com/training/sharing/receive.html
既然你的應用可以向其它應用發送數據,那么你的應用也可以接收來自其它應用的數據。您需要思考一下用戶是如何與你的應用交互的,以及你希望接收來自其它應用什么樣的數據。例如,一個社交網絡應用可能會期望收到來自其它應用的文本內容,比如一個感興趣的網頁URL。而 Google+ Android application 即接收文本和單個或多個圖像。這樣一來,用戶就可以輕松的再Google+上發布來自Android圖庫應用中的照片了。
?
一). 更新你的清單文件
intent過濾器會告知系統,應用組件期望接收什么樣的intents。如在課程 Sending Simple Data to Other Apps (博客鏈接: http://www.cnblogs.com/jdneo/p/3473170.html )你構造一個具有 ACTION_SEND 的intent類似。你可以創建intent過濾器來接收這一行為的intent。你在你的清單文件中使用 <intent-filter> 標簽定義一個intent過濾器。例如,如果你的應用能處理接收文本內容,一個單一的任何類型的圖片,或者多張不同類型的圖片,你的清單文件看上去應該是這樣的:
<
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
>
Note:
更多intent過濾器以及intent解析的內容,可以閱讀: Intents and Intent Filters
當另一個應用通過構造一個intent并且將它傳遞給 startActivity() ,來嘗試分享任何這些數據時,你的應用將會在intent選擇器中列出來。如果用戶選擇了你的應用,響應的activity(在上例中是“ .ui.MyActivity ” )將會被啟動。之后合理地處理內容就要看你的代碼和UI的設計了。
?
二). 處理接收的內容
為了處理 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
}
}
Caution:
檢驗接收的數據要額外關注,因為你永遠都預測不到其他應用會發給你什么數據。例如,錯誤的MIME類型可能被設置,或者發送的圖片可能特別大。另外,要記住在另一個線程執行二進制數據,而不是在主線程(“ UI線程 ”)。
更新一個UI可能簡單到填充一個 EditText ,也有可能復雜到在一幅圖片上應用一個濾鏡效果。下一步如何執行完全取決于你的應用。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

