现在的位置: 首页 > 移动开发> 正文
Android 信息共享专题之内容接收
2012年01月07日 移动开发 暂无评论 ⁄ 被围观 4,202+

如上节课《Android 信息共享之内容发布》所讲,您的应用可以给其他的APP发送内容,同样地,您的应用也可以接收和处理其他APP发送的内容。设计之初,您就应该考虑,如何让您的APP与用户交互,以及您的APP主要处理哪些类型的数据。例如,一个网络社交类的APP可能对纯文本格式的数据感兴趣,像网页的URL地址。 Google+ Android application 则同时对纯文本格式的数据和单张或多张图片类型的数据感兴趣,因为这样可以方便用户在 Gallery APP 浏览图片的时候发送一条附带图片的 Google+ 信息流(Post)。


更新 Manifest 文件

Intent filters 主要用来告诉系统您的APP关心什么类型的系统消息。如前面章节《Android 信息共享之内容发布》构造发送内容的Intent时需要设置Action为ACTION_SEND,那么接收内容同样要告诉系统您的APP对Action为ACTION_SEND的系统消息感兴趣。您需要在 <intent-filter> tag中定义关注的intent filter,比如,您的APP可以用来处理纯文本格式的数据,通用类型的单张图片,或者混合类型的图片列表,那么您的manifest 可能是这样的:

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

:想了解更多的有关Intent和Intent Filter的资讯请参照 《 Intents and Intent Filters》。

假如其他的APP发送了与上面manifest文件中声明的intent filter匹配的数据(通过声明Intent,设置Action值,填充数据,调用startActivity()方法),那么您的APP就会出现在 intent chooser(选择器)中,当用户选择了您的应用程序,对应的activity (如上文声明的 .ui.MyActivity)就会被启动,具体怎么样处理接收到的数据就取决于您的设计了。

处理接收到的数据

要想得到其他APP发送过来的数据,您首先需要调用方法getIntent() 来获得包含数据的Intent对象,一旦您得到了该对象,就可以读取里面的值来决定下一步的动作。有一点您需要注意,如果您的Activity的启动入口不止这一处,比如用户可以通过主界面launcher启动,那么您就应该小心应对此类问题,主要区别是两者 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
      }
   }

注意 :对于从其他APP发送过来的数据,您需要特别小心地检查和处理,因为您不可能提前预知它们会发送什么样的数据。例如,它们有可能设置了错误的MIME 类型,或者传递一张非常大的图片,还有一点需要注意,处理二进制数据的时候最好新启一个独立的线程来处理,而不要放在主线程(main ("UI") thread)中,防止界面阻塞。

更新UI界面可以很简单,比如将接收到的纯文本格式的数据显示到界面的EditText控件中,也可以很复杂,比如对接收到的图片数据做图像的变换处理,然后再显示。具体进行怎样的处理各不相同,这要看您APP的设计了。

参考文摘:

http://developer.android.com/training/sharing/receive.html

给我留言

留言无头像?


×
腾讯微博