转载:隐式Intent
一、隐式意图介绍
显式意图我们前面已经提到,形如:
Intent intent = new Intent();
intent.setClass(this,Other.class); //此句表示显式意图,因为明确设置激活对象为Other类
startActivity(intent);
顾名思义,隐式意图就是在不明确设置激活对象的前提下寻找最匹配的组件,举个例子,比如有5个人:
(1)A:170cm
(2)B:160cm
(3)C:180cm
(4)D:190cm
(5)E:200cm
如果是显示意图的话,如果我们要指明选择A的话会说:”我选择A.“,但是如果是隐式意图,则会说:”我要选择170cm的人“,虽然没有指明要选A,但会寻找条件最匹配的人。
在intent过滤器中类似于上面例子中的”身高“条件的匹配条件有:
(1)action
(2)category
(3)data:scheme、host、path、type
当在程序中设置了这些激活组件的条件,程序就会去寻找最匹配的组件,但是注意:只要有一点不匹配,则就是不匹配;
比如:
Intent intent = new Intent();
intent.setAction("a"); //此句只是指定了Action
startActivity(intent); //寻找最匹配的组件激活,内部会调用intent.addCategory("android.intent.category.DEFAULT");
二、隐式Intent的核心代码
首先是在AndroidManifest.xml中为某个Activity设置意图过滤器:
<activity>
<intent-filter>
<action android:name="...."/>
<category android:name="...."/>
<category android:name="android.intent.category.DEFAULT"/> <!--此句一般都要加 -->
<data android:scheme="..." android:host="..." android:path="/..." android:type="..."/>
</intent-filter>
</activity>
以上设置是设置Activity本身的属性,接下来在程序中要设置的是我们要寻找时匹配的条件:
(1)Intent intent = new Intent();
(2)intent.setAction("....");
(3)intent.addCategory("....");
(4)intent.setData(Uri.parse("....")); //设置data的scheme、host、path条件
(5)intent.setDataAndType(Uri.parse(""),String type); //同时设置data的scheme、host、path、type条件
(6)startActivity(intent); //调用intent.addCategory("android.intent.category.DEFAULT");
三、代码举例
场景介绍:在MainActivity中有一个按钮,点击按钮后,会进行隐式Intent匹配,最后寻找到并激活OtherActivity.
情况1:
OtherActivity
则代码为:
Intent intent = new Intent();
intent.setAction("com.xiazdong.action");
intent.addCategory("com.xiazdong.category");
intent.setData(Uri.parse("xiazdong://www.xiazdong.com/xia"));
startActivity(intent); //此方法中调用intent.addCategory("android.intent.category.DEFAULT");
情况2:
在<data>中多了一个android:mimeType="text/*",此时不能使用intent.setData,而要使用intent.setDataAndType();
<activity
android:name=".OtherActivity"
android:label="OtherActivity" >
<intent-filter>
<action android:name="com.xiazdong.action" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.xiazdong.category" />
<data
android:host="www.xiazdong.com"
android:scheme="xiazdong" android:mimeType="text/*"/>
</intent-filter>
</activity>
代码为:
Intent intent = new Intent();
intent.setAction("com.xiazdong.action");
intent.addCategory("com.xiazdong.category");
intent.setDataAndType(Uri.parse("xiazdong://www.xiazdong.com/xia"), "text/plain"); //匹配了text/*
startActivity(intent); //此方法中调用intent.addCategory("android.intent.category.DEFAULT");
转载:隐式Intent的更多相关文章
- Android开发学习笔记:浅谈显示Intent和隐式Intent
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/655132 ...
- 在Android中Intent的概念及应用(一)——显示Intent和隐式Intent
Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...
- 显式Intent和隐式Intent
http://blog.csdn.net/qs_csu/article/details/7995966 对于明确指出了目标组件名称的Intent,我们称之为“显式Intent”. 对于没有明确指出目标 ...
- Android开发之隐式Intent中Intent-filter的三个属性-action,category,data
使用隐式Intent时,需要使用到意图过滤器Intent-filter.Intent-filter含有三个属性:action,category,data.通过这三个属性的组合,可以启动想要启动的act ...
- 隐式intent
使用隐式Intent,我们不仅可以启动自己程序内的活动,还可以启动其他程序的活动, 这里我们首先指定了Intent的action是Intent.ACTION_VIEW,这是一个Android系统内置的 ...
- android隐式intent使用场景解析
Android 隐式intent相信大家都有用过,大部分场景我们用显式intent已经能满足我们的业务需求,隐式intent大部分都是用来启动系统自带的Activity或Service之类的组件.昨天 ...
- android 在5.0以后不允许使用隐式Intent方式来启动Service
android5.0以后不能使用隐式intent :需要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage(& ...
- 第一行代码阅读笔记----显示隐式Intent的基本用法
1.显示Intent意图明显,通过Intent启动另外一个活动,这是安卓中各组件进行交互的一种重要方式.一般用于启动活动,启动服务,发送广播等场景. 实现方法,这里我只说思路,实践还是要自己实操才能明 ...
- 学习安卓开发[4] - 使用隐式Intent启动短信、联系人、相机应用
在上一篇学习安卓开发[3] - 使用RecyclerView显示列表中了解了在进行列表展示时RecyclerView的使用,本次记录的是在应用中如何通过隐式Intent调用其它应用的功能,比如发短信. ...
随机推荐
- 快速找出System.Management.Automation.dll,c#调用powershell
public static void InvokeSystemPS(string cmd) { List<string> ps = new List<string>(); ps ...
- 【Java面试题】11 什么是内部类?Static Nested Class 和 Inner Class的不同。
Inner Class(内部类)定义在类中的类. (一般是JAVA的说法) Nested Class(嵌套类)是静态(static)内部类.(一般是C++的说法)静态内部类:1 创建一个static内 ...
- Making the iPhone vibrate (iPhone 振动)
from: http://stackoverflow.com/a/4725039 There are two seemingly similar functions that take a param ...
- Buff系统的实现
BUFF是很多游戏都在采用的一种临时增益机制.本文讲述如何在基于关系型数据库的网页游戏中实现这一系统:如何扩展该系统:以及如何提高该系统的性能. 引言 BUFF是很多游戏都在采用的一种临时增益机制:与 ...
- ADO 调用Execute失败,异常码DB_E_DATAOVERFLOW
今天,通过ADO接口往PG数据库中插入数据,结果数据始终不能插入到数据库的表中,执行insert语句后,返回失败,错误码DB_E_DATAOVERFLOW. DB_E_DATAOVERFLOW:命令中 ...
- SqlBulkCopy类进行大数据(10000万条以上)插入测试
好多天没写博客了,刚刚毕业一个多月,在IT的路上真是迷茫啊! 关于上一篇博客中提到的,在进行批量数据插入数据库的时候可以通过给存储过程传递一个类型为Table的参数进行相关操作,在这个过程中本人没有进 ...
- ASP.Net MVC开发基础学习笔记(7):数据查询页面
前言 前面铺垫了那么多,今天我们就用MVC5 + EF6 + Bootstrap3来正式创建一个基本查询页面. 为什么从查询页面開始?嘿嘿.由于小弟的.Net生涯就是从查询页面開始的,记得正式工 ...
- VirtualBox设置NAT端口映射
原文地址 :http://www.2cto.com/os/201209/153863.html VirtualBox设置NAT端口映射 好吧,我知道这个问题有很多人都讲过,但是,你们不觉得VB ...
- html5 file 自定义文件过滤
使用 acctpe属性即可 示例: gif,jpg <input type="file" name="pic" accept="image/gi ...
- hadoop程序MapReduce之DataDeduplication
需求:去掉文件中重复的数据. 样板:data.log 2016-3-1 a 2016-3-2 b 2016-3-2 c 2016-3-2 b 输出结果: 2016-3-1 a 2016 ...