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
  1. <activityandroid:name=".ui.MyActivity">
  2. <intent-filter>
  3. <actionandroid:name="android.intent.action.SEND"/>
  4. <categoryandroid:name="android.intent.category.DEFAULT"/>
  5. <dataandroid:mimeType="image/*"/>
  6. </intent-filter>
  7. <intent-filter>
  8. <actionandroid:name="android.intent.action.SEND"/>
  9. <categoryandroid:name="android.intent.category.DEFAULT"/>
  10. <dataandroid:mimeType="text/plain"/>
  11. </intent-filter>
  12. <intent-filter>
  13. <actionandroid:name="android.intent.action.SEND_MULTIPLE"/>
  14. <categoryandroid:name="android.intent.category.DEFAULT"/>
  15. <dataandroid:mimeType="image/*"/>
  16. </intent-filter>
  17. </activity>
<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的时候考虑这种情况(是被其他程序而调用启动的)。
  1. void onCreate (Bundle savedInstanceState) {
  2. ...
  3. // Get intent, action and MIME type
  4. Intent intent = getIntent();
  5. String action = intent.getAction();
  6. String type = intent.getType();
  7. if (Intent.ACTION_SEND.equals(action) && type != null) {
  8. if ("text/plain".equals(type)) {
  9. handleSendText(intent); // Handle text being sent
  10. } elseif (type.startsWith("image/")) {
  11. handleSendImage(intent); // Handle single image being sent
  12. }
  13. } elseif (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
  14. if (type.startsWith("image/")) {
  15. handleSendMultipleImages(intent); // Handle multiple images being sent
  16. }
  17. } else {
  18. // Handle other intents, such as being started from the home screen
  19. }
  20. ...
  21. }
  22. void handleSendText(Intent intent) {
  23. String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
  24. if (sharedText != null) {
  25. // Update UI to reflect text being shared
  26. }
  27. }
  28. void handleSendImage(Intent intent) {
  29. Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
  30. if (imageUri != null) {
  31. // Update UI to reflect image being shared
  32. }
  33. }
  34. void handleSendMultipleImages(Intent intent) {
  35. ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
  36. if (imageUris != null) {
  37. // Update UI to reflect multiple images being shared
  38. }
  39. }
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,谢谢!

android 从其他app接收分享的内容的更多相关文章

  1. Android中实现APP文本内容的分享发送与接收方法简述

    谨记(指定选择器Intent.createChooser()) 开始今天的内容前,先闲聊一下: (1)突然有一天头脑风暴,对很多问题有了新的看法和见解,迫不及待的想要分享给大家,文档已经写好了,我需要 ...

  2. Android分享内容和接收分享内容小小实现

    先来说说分享,毕竟没有分享何来接收分享可谈? 分享目前已实现的有两种方式:后台代码实现.ShareActionProvider实现,接着先说通过代码实现 Intent intent=new Inten ...

  3. 浅谈APP的分享功能,有时候社交裂变形式比内容更“重要”

    回顾2018年的移动互联网,“社交裂变”“下沉”等成为年度关键词.一方面我们可以看到社交裂变助推用户增长,另一方面我们也看到了以拼多多.趣头条为代表的互联网企业对于社交裂变模式表现出的空前关注度.作为 ...

  4. Android原生APP内分享

    Android原生APP内分享,可实现数据分享以及assets文件夹分享及私有文件分享 项目地址:https://github.com/json-pu/AndroidAppShare.git

  5. Android中怎样做到自己定义的广播仅仅能有指定的app接收

    今天没吊事.又去面试了,详细哪家公司就不说了,由于我在之前的blog中注明了那些家公司的名字,结果人家给我私信说我泄露他们的题目.好吧,我错了... 事实上当我们已经在工作的时候.我们能够在空暇的时间 ...

  6. Android经典项目开发之天气APP实例分享

    原文:Android经典项目开发之天气APP实例分享 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/mzc186/article/details/5 ...

  7. Android中如何做到自定义的广播只能有指定的app接收

    今天没吊事,又去面试了,具体哪家公司就不说了,因为我在之前的blog中注明了那些家公司的名字,结果人家给我私信说我泄露他们的题目,好吧,我错了...其实当我们已经在工作的时候,我们可以在空闲的时间去面 ...

  8. Android - 和其他APP交互 - 把用户带到其他app

    Android的重要功能之一就是app可以根据要执行的操作让用户启动另外一个app.例如,app有一个商业地址然后想要在地图上显示,并不需要在app中加一个显示地图的activity,可以直接用Int ...

  9. 如何给自己的app添加分享到有道云笔记这样的功能

    文章同步自http://javaexception.com/archives/34 如何给自己的app添加分享到有道云笔记这样的功能 问题: 在之前的一个开源笔记类项目Leanote中,有个用户反馈想 ...

随机推荐

  1. C++创建对象的三种方式

    C++在创建对象的时候,有三种方式: #include <iostream> using namespace std; class A { private: int n; public: ...

  2. linux分区工具fdisk的使用

    fdisk是linux下的一块分区工具,使用简单方便,由于是对系统进行修改,需要root权限. 常用参数如下: fdisk  -l : 列出所有的硬盘信息 直接传入设备名称可进入对该硬盘分区.例如,f ...

  3. mac下 配置 Apache Php Mysql

    参考 http://www.guomii.com/posts/30136 参考 http://forums.mysql.com/read.php?11,600754,600754 MacOS 10.8 ...

  4. WebRTC–getUserMedia & Canvas

    下面是一个使用getUserMedia接口和Canvas的drawImage方法实现的截图功能(截取视频中的一帧). 基本思路是这样子的: getUserMedia获取一个MediaStream, s ...

  5. [Oracle] 11G自己主动收集统计信息

    在11g中,默认自己主动收集统计信息的时间为晚上10点(周一到周五,4个小时),早上6点(周六,周日,20个小时),例如以下所看到的: select a.window_name, a.repeat_i ...

  6. C++之字符串分割函数split

    c++之字符串分割: /* *c++之字符串分割: */ #include <iostream> #include <string> #include <vector&g ...

  7. ThinkPHP - 连贯操作 - 【实现机制】

    <?php //模型类 class Model { //数据库连接 private $_conn = NULL; //where语句 private $_where = NULL; //表名称 ...

  8. 在Mac上配置/使用Github

    文/天才晓波(简书作者)原文链接:http://www.jianshu.com/p/20eee155bbee著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 先简单介绍一下Git和Git ...

  9. nginx前端负载,后端apache获取真实IP设置

    原文链接: nginx前端负载,后端apache获取真实IP设置 参考文献: 前端Nginx,后端Apache获取用户真实IP地址  按照第二种方法设置不成功! 网站最前端是nginx,做的PROXY ...

  10. ECSHOP常用函数

    lib_time.php gmtime() #获得当前格林威治时间的时间戳 /$0 server_timezone() #获得服务器的时区 /$0 local_mktime($hour = NULL ...