【Android Developers Training】 33. 接收来自其它应用的简单数据
注:本文翻译自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,也有可能复杂到在一幅图片上应用一个滤镜效果。下一步如何执行完全取决于你的应用。
【Android Developers Training】 33. 接收来自其它应用的简单数据的更多相关文章
- 【Android Developers Training】 32. 向其它应用发送简单数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 26. 在SQL数据库中保存数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 31. 序言:共享简单数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 23. 序言:保存数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 42. 从另一台设备接收文件
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 108. 使用模拟定位进行测试
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 106. 创建并检测地理围栏
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 54. 打印自定义文档
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 29. 从Activity获得结果
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
随机推荐
- 关于java中的==,equals()
1. 先从一道面试题说起 请问下面的 public class Demo { public static void main(String args[]){ String a = "a&qu ...
- (转载)sizeof
[C++专题]C++ sizeof 使用规则及陷阱分析 摘要:鉴于sizeof为各大软件公司笔试.面试必考题,现收集sizeof的各种用法,尽量做到全面理解,其中例子希望能举一反三.提示:下文例子 ...
- jQuery选择器中的空格问题
前几天就遇到过这样的问题,明明我用的是('tr :even').css('background','#ccc')想改变表格中行的背景色,反复试了还是没改变.还问了度娘还是没找到原因所在(当时问题描述的 ...
- Easy machine learning pipelines with pipelearner: intro and call for contributors
@drsimonj here to introduce pipelearner – a package I'm developing to make it easy to create machine ...
- 客户端存储 API
介绍两个在客户端存储数据的 API localStorage与sessionStorage 两个都是window对象的属性,利用这两个属性,可以在客户端存储一些数据 相比cookie,这种存储方式的优 ...
- 弹出框插件layer使用
layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. 插件官方地址:http://layer.layui.co ...
- HttpClien Get&Post
新公司上班第二周,开始进军.Net Core,这方面的东西比较新,所以已经封装好的东西比较少,比如HttpClien之类的开源类库,找了NuGet好久,没有找到,所以先写个简陋的来用着先. 引用: u ...
- kotlin成长之路
前言: 从接触Kotlin开始,也就是我今天开启写技术博客的决定,文采不佳,欢迎各位阅读者的理解与指点.而该篇文章是最为博客新手的我对Kotlin成长的引导篇,所以内容一般是Kotlin技术博客的目录 ...
- Docker化tomcat 并且使用maven热部署
今天没事干 就把自己的tomcat docker化并且使用maven实现热部署 特别记录一下~ 1.首先进行centos7的安装 就不在记录了 注意的是最好使用centos7 因为docker 依赖 ...
- MySQL 5.7中 performance_schema 替代 show profile 命令
本文出处:http://www.cnblogs.com/wy123/p/6979499.html show profile 命令用于跟踪执行过的sql语句的资源消耗信息,可以帮助查看sql语句的执行情 ...