注:本文翻译自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 ,也有可能复杂到在一幅图片上应用一个滤镜效果。下一步如何执行完全取决于你的应用。

