当前位置: 首页 > news >正文

【Android Advanced Training - 07】分享数据内容 [Lesson 2 - 从其它app接收分享的内容]...

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
<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的时候考虑这种情况(是被其他程序而调用启动的)。
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 Advanced Training - 07】分享数据内容 [Lesson 3 - 在ActionBar上添加Share Action]...
  • 憋了好几年的引擎终于露面了
  • 20154307《网络对抗》Exp8 Web基础
  • Bash知识点记录
  • Android 滑动效果入门篇(一)—— ViewFlipper
  • 在Moto Atrix4G手机里安装全功能Ubuntu记录(特附友情广告一则)
  • rpm 软件包管理
  • pom.xml文件详解
  • 互联网测试有什么不一样
  • Python基础第十六天:面向对象进阶
  • log4net 在.net CompactFramework 2.0中的使用
  • 20165307 实验四《Andriid应用开发》实验报告
  • 使用 php Header 报错的一个原因
  • 【IOS】仿捕鱼达人的金币滚动显示
  • 谷歌 AXURE RP EXTENSION拓展问题
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • C++11: atomic 头文件
  • Git初体验
  • KMP算法及优化
  • Linux中的硬链接与软链接
  • SpiderData 2019年2月16日 DApp数据排行榜
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 机器学习中为什么要做归一化normalization
  • 区块链技术特点之去中心化特性
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 入口文件开始,分析Vue源码实现
  • 使用API自动生成工具优化前端工作流
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • 容器镜像
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (js)循环条件满足时终止循环
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (附源码)SSM环卫人员管理平台 计算机毕设36412
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (一)SpringBoot3---尚硅谷总结
  • (转)Oracle存储过程编写经验和优化措施
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .net 使用ajax控件后如何调用前端脚本
  • .考试倒计时43天!来提分啦!
  • @在php中起什么作用?
  • [ vulhub漏洞复现篇 ] Jetty WEB-INF 文件读取复现CVE-2021-34429
  • [acwing周赛复盘] 第 94 场周赛20230311
  • [C#]使用PaddleInference图片旋转四种角度检测
  • [CTO札记]如何测试用户接受度?
  • [Fri 26 Jun 2015 ~ Thu 2 Jul 2015] Deep Learning in arxiv
  • [Kubernetes]9. K8s ingress讲解借助ingress配置http,https访问k8s集群应用
  • [LeetCode 127] - 单词梯(Word Ladder)
  • [LeetCode] 626. 换座位
  • [Lucene] Lucene 全文检索引擎简介
  • [luogu2165 AHOI2009] 飞行棋 (枚举)
  • [MFC] VS2013版本MFC工程移植到VC6.0上
  • [one_demo_8]十进制转二进制