Android - 分享内容 - 接收其他APP的内容
就象程序可以发送数据给其他程序,所以也可以接收其他程序的数据。想一下用户如何和程序交互,以及想从其他程序接收什么样类型的数据。例如,一个社交程序可能对接收其他程序的文字(比如有趣的网址)感兴趣。Google+ 程序可接收文字和单多张图片。用这个app,用户可以和容易的用Android Gallery中的相片在Google+上发布。
更新Manifest
intent filter会告诉系统程序会打算接收什么。就和前面讲的如何用ACTION_SEND创建intent相似,创建intent filter来接收带有这个操作的intent。在manifest中用<intent-filter>元素来来定义一个intent filter。例如,如果程序可接收文字,任何类型的单张图片,或任何类型的多张图片,mainfest应该象:
<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 filters和intetent解决方案请参考Intents and Intent Fileters
当其他程序通过创建intent然后传递给startActivity()来分享上面的类容,你的程序会在intent chooser列表中显示,如果用户选择了你的程序,相应的activity(上面例子中的.ui.MyActivity)将会被启动。然后就由你来在代码和界面中来处理内容了。
处理传入的数据
要处理Intent传递的数据,首先调用getIntent()来获得Intent对象。一旦获得了这个对象,可以通过查看数据来决定接下来怎么做。记住如果activity可以从系统的其他部分启动,比如launcher,那么需要在查看intent的时候考虑这些情况。
void onCreate (Bundle savedInstanceState) {
...
// 获得 intent, action 和 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); // 处理发送来的文字
} else if (type.startsWith("image/")) {
handleSendImage(intent); // 处理发送来的图片
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent); // 处理发送来的多张图片
}
} else {
// 处理其他intents,比如由主屏启动
}
...
} void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// 根据分享的文字更新UI
}
} void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// 根据分享的图片更新UI
}
} void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// 根据分享的多张图片更新UI
}
}
注意:要格外小心的检查传入的数据,你不知道其他程序传进来什么。例如,有可能设置了错的MIME类型,或者图片可能非常大。还要记住,在另外一个线程中处理二进制数据,而不是UI线程。
更新UI可以是像填充EditText一样简单,或者更难一些像在一张图片上添加一个有趣的滤镜。由你的程序来决定接下来会发生什么。
上一篇:Android - 分享内容 - 给其他APP发送内容
下一篇:Android - 分享内容 - 添加一个简单的分享操作
Android - 分享内容 - 接收其他APP的内容的更多相关文章
- Android - 分享内容 - 给其他APP发送内容
创建一个intent时,必须要指定intent将要触发的操作.Android定义了很多操作,包括ACTION_SEND,就象可以猜到的一样,表示intent是把数据从一个activity发送给另一个, ...
- Android - 分享内容
Android程序一个很重要地特性是他们可以相互通讯和交互.为什么要重新开发那些不是你程序的核心而且其他程序已经有的功能呢? 这里将要介绍一些使用Intent API和ActionProvider对象 ...
- Android中实现APP文本内容的分享发送与接收方法简述
谨记(指定选择器Intent.createChooser()) 开始今天的内容前,先闲聊一下: (1)突然有一天头脑风暴,对很多问题有了新的看法和见解,迫不及待的想要分享给大家,文档已经写好了,我需要 ...
- Android分享内容和接收分享内容小小实现
先来说说分享,毕竟没有分享何来接收分享可谈? 分享目前已实现的有两种方式:后台代码实现.ShareActionProvider实现,接着先说通过代码实现 Intent intent=new Inten ...
- iOS开发之集成iOS9中的Core Spotlight Framework搜索App的内容
Spotlight在iOS9上做了一些新的改进, 也就是开放了一些新的API, 通过Core Spotlight Framework你可以在你的app中集成Spotlight.集成Spotlight的 ...
- Android基础笔记(十四)- 内容提供者读取联系人
利用内容提供者读取联系人 利用内容提供者插入联系人 内容观察者的原理 利用内容观察者监听系统应用数据库或者自己应用数据库的变化 利用内容提供者读取联系人 读取联系人相对于读取短信来说就复杂非常多了,我 ...
- 【网站公告】请大家不要发表任何涉及“得到App”的内容
大家好,今天我们收到来自杭州某某网络科技有限公司的维权骑士团队的邮件,说他们受某某(天津)文化传播有限公司委托,展开维权.园子里有些博主因为学习“得到App”的课程在博客中记了一些笔记,也被维权. 为 ...
- android ----- 分享的连接在手机上打开App
首先做成HTML的页面,页面内容格式如下: <a href="[scheme]://[host]/[path]?[query]">启动应用程序</a> 这一 ...
- python 全栈开发,Day127(app端内容播放,web端的玩具,app通过websocket远程遥控玩具播放内容,玩具管理页面)
昨日内容回顾 1. 小爬爬 内容采集 XMLY 的 儿童频道 requests 2. 登陆 注册 自动登陆 退出 mui.post("请求地址",{数据},function(){} ...
随机推荐
- 使用 JQueryMobile 点击超链接提示“error loading page” 错误
使用jquery mobile创建dialog时出现加载错误,“Error Loading Page”. 原因是:jquery mobile页面默认采用ajax方式进行交互,而ajax方式下是不支持f ...
- A Game of Thrones(9) - Tyrion
Somewhere in the great stone maze(迷宫:迷惑) of Winterfell, a wolf howled. The sound hung over the castl ...
- UVA 11090 - Going in Cycle!!(Bellman-Ford)
UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...
- 开源一个适用iOS的数据库表结构更新机制的代码
将前段时间开源的代码.公布一下: ARDBConfig On the iOS, provide a database table structure update mechanism, ensure ...
- Android四个多线程分析:MessageQueue实现
Android四个多线程分析:MessageQueue的实现 罗朝辉 (http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 在前面两篇文章<Android多线 ...
- Spring MVC中一般 普通类调用service
在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...
- 八月份 CUGBACM_Summer_Tranning 题解
CUGBACM_Summer_Tranning4 比赛链接:http://vjudge.net/contest/view.action?cid=52230#overview 题解链接: F . HDU ...
- Android数据库高手秘籍(五)——LitePal的存储操作
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/39345833 经过前面几篇文章的学习,我们已经把LitePal的表管理模块的功能都 ...
- Conversion to Dalvik format failed with error 1
主要和添�的第三方的包有关系. ======================================= 出现,Conversion to Dalvik format failed with e ...
- oracle nologging用法(转)
一.oracle日志模式分为(logging,force logging,nologging) 默认情况是logging,就是会记录到redo日志中,force logging是强制记录日志,nolo ...