Android - 分享内容 - 接收其他APP的内容

就象程序可以发送数据给其他程序,所以也可以接收其他程序的数据。想一下用户如何和程序交互,以及想从其他程序接收什么样类型的数据。例如,一个社交程序可能对接收其他程序的文字(比如有趣的网址)感兴趣。Google+ 程序可接收文字和单多张图片。用这个app,用户可以和容易的用Android Gallery中的相片在Google+上发布。

更新Manifest

intent filter会告诉系统程序会打算接收什么。就和前面讲的如何用ACTION_SEND创建intent相似,创建intent filter来接收带有这个操作的intent。在manifest中用<intent-filter>元素来来定义一个intent filter。例如,如果程序可接收文字,任何类型的单张图片,或任何类型的多张图片,mainfest应该象:

 1 <activity android:name=".ui.MyActivity" >
 2     <intent-filter>
 3         <action android:name="android.intent.action.SEND" />
 4         <category android:name="android.intent.category.DEFAULT" />
 5         <data android:mimeType="image/*" />
 6     </intent-filter>
 7     <intent-filter>
 8         <action android:name="android.intent.action.SEND" />
 9         <category android:name="android.intent.category.DEFAULT" />
10         <data android:mimeType="text/plain" />
11     </intent-filter>
12     <intent-filter>
13         <action android:name="android.intent.action.SEND_MULTIPLE" />
14         <category android:name="android.intent.category.DEFAULT" />
15         <data android:mimeType="image/*" />
16     </intent-filter>
17 </activity>

注意:更多关于intent filters和intetent解决方案请参考Intents and Intent Fileters

当其他程序通过创建intent然后传递给startActivity()来分享上面的类容,你的程序会在intent chooser列表中显示,如果用户选择了你的程序,相应的activity(上面例子中的.ui.MyActivity)将会被启动。然后就由你来在代码和界面中来处理内容了。

处理传入的数据

要处理Intent传递的数据,首先调用getIntent()来获得Intent对象。一旦获得了这个对象,可以通过查看数据来决定接下来怎么做。记住如果activity可以从系统的其他部分启动,比如launcher,那么需要在查看intent的时候考虑这些情况。

 1 void onCreate (Bundle savedInstanceState) {
 2     ...
 3     // 获得 intent, action 和 MIME type
 4     Intent intent = getIntent();
 5     String action = intent.getAction();
 6     String type = intent.getType();
 7 
 8     if (Intent.ACTION_SEND.equals(action) && type != null) {
 9         if ("text/plain".equals(type)) {
10             handleSendText(intent); // 处理发送来的文字
11         } else if (type.startsWith("image/")) {
12             handleSendImage(intent); // 处理发送来的图片
13         }
14     } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
15         if (type.startsWith("image/")) {
16             handleSendMultipleImages(intent); // 处理发送来的多张图片
17         }
18     } else {
19         // 处理其他intents,比如由主屏启动
20     }
21     ...
22 }
23 
24 void handleSendText(Intent intent) {
25     String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
26     if (sharedText != null) {
27         // 根据分享的文字更新UI
28     }
29 }
30 
31 void handleSendImage(Intent intent) {
32     Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
33     if (imageUri != null) {
34         // 根据分享的图片更新UI
35     }
36 }
37 
38 void handleSendMultipleImages(Intent intent) {
39     ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
40     if (imageUris != null) {
41         // 根据分享的多张图片更新UI
42     }
43 }

注意:要格外小心的检查传入的数据,你不知道其他程序传进来什么。例如,有可能设置了错的MIME类型,或者图片可能非常大。还要记住,在另外一个线程中处理二进制数据,而不是UI线程。

更新UI可以是像填充EditText一样简单,或者更难一些像在一张图片上添加一个有趣的滤镜。由你的程序来决定接下来会发生什么。

 

上一篇:Android - 分享内容 - 给其他APP发送内容

下一篇:Android - 分享内容 - 添加一个简单的分享操作

posted @ 2013-06-21 16:59  fengquanwang  阅读(3840)  评论(1编辑  收藏  举报