一、隐式意图介绍

显式意图我们前面已经提到,形如:

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的更多相关文章

  1. Android开发学习笔记:浅谈显示Intent和隐式Intent

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/655132 ...

  2. 在Android中Intent的概念及应用(一)——显示Intent和隐式Intent

    Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...

  3. 显式Intent和隐式Intent

    http://blog.csdn.net/qs_csu/article/details/7995966 对于明确指出了目标组件名称的Intent,我们称之为“显式Intent”. 对于没有明确指出目标 ...

  4. Android开发之隐式Intent中Intent-filter的三个属性-action,category,data

    使用隐式Intent时,需要使用到意图过滤器Intent-filter.Intent-filter含有三个属性:action,category,data.通过这三个属性的组合,可以启动想要启动的act ...

  5. 隐式intent

    使用隐式Intent,我们不仅可以启动自己程序内的活动,还可以启动其他程序的活动, 这里我们首先指定了Intent的action是Intent.ACTION_VIEW,这是一个Android系统内置的 ...

  6. android隐式intent使用场景解析

    Android 隐式intent相信大家都有用过,大部分场景我们用显式intent已经能满足我们的业务需求,隐式intent大部分都是用来启动系统自带的Activity或Service之类的组件.昨天 ...

  7. android 在5.0以后不允许使用隐式Intent方式来启动Service

    android5.0以后不能使用隐式intent :需要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage(& ...

  8. 第一行代码阅读笔记----显示隐式Intent的基本用法

    1.显示Intent意图明显,通过Intent启动另外一个活动,这是安卓中各组件进行交互的一种重要方式.一般用于启动活动,启动服务,发送广播等场景. 实现方法,这里我只说思路,实践还是要自己实操才能明 ...

  9. 学习安卓开发[4] - 使用隐式Intent启动短信、联系人、相机应用

    在上一篇学习安卓开发[3] - 使用RecyclerView显示列表中了解了在进行列表展示时RecyclerView的使用,本次记录的是在应用中如何通过隐式Intent调用其它应用的功能,比如发短信. ...

随机推荐

  1. am335x 10.1"电容touch 不能识别

    /**************************************************************** * am335x 10.1"电容touch 不能识别 * ...

  2. e636. Listening to All Key Events Before Delivery to Focused Component

    Registering a key event dispatcher with the keyboard focus manager allows you to see all key events ...

  3. poj2513(无向图判欧拉路)

    链接:id=2513">点击打开链接 题意:一堆木棍左右两端涂有颜色,同样颜色的能够连接在一起,问全部木棍是否能都连上 代码: #include <map> #includ ...

  4. jquery widgets 弹框

    <div id='dialog' style="display:none;"> <div style="text-align:center;" ...

  5. NPOI帮助类(Excel转DataTable、DataTable转Excel)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...

  6. vs2013配置opencv3.2.0

    工具/原料 l VS2013 l OpenCV3.20http://jaist.dl.sourceforge.net/project/opencvlibrary/opencv-win/3.2.0/op ...

  7. How to Setup Cordova for Windows 7

    Setup Cordova Text Editor / IDE You may need to prepare an IDE or Editor for working. Here for examp ...

  8. centos无法联网解决方法

    1)进入 /etc/sysconfig/network-scripts/ 2)vi 或 vim 打开 ifcfg-eth0(不一定是eth0,这个自己判断了) 3)将 ONBOOT=no 改成 ONB ...

  9. docker学习-docker仓库

    docker仓库中心:https://hub.docker.com/ 网易蜂巢仓库中心:https://c.163.com/hub#/m/home/

  10. windows命令之PING DIR DEL CD TASKLIST (转)

    最简单的莫过于PING命令了. PING命令的功能就是给对方主机发送IP数据包. 一般都是测试主机是否在线. 用法如下: PING 192.168.1.1.PING命令默认发送的是四个数据包,当然也可 ...