Intent显式调用看这里:Android入门练习——Intent的显式调用
隐式Intent
不明确指定启动哪个组件,而是让系统来筛选出合适的Activity。
使用隐式Intent的时候,系统通过将Intent对象中的IntentFilter与组件在AndroidManifest.xml或者代码中动态声明的IntentFilter进行比较,从而找到要启动的相应组件。如果组件的IntentFilter与Intent中的IntentFilter正好匹配,系统就会启动该组件,并把Intent传递给它。如果有多个组件同时匹配到了,则系统会弹出一个选择框,让用户选择使用哪个应用去处理这个Intent。
<intent-filter>……</intent-filter>
里定义的所有内容都是用来定义该activity可以被哪些intent激活的,如果匹配,就会被激活。
在<intent-filter>
里有以下几个属性可以让intent来匹配:Action、Category、Data;
-
Action:该activity可以执行的动作
如果跟这里<intent-filter>
所列出的任意一个匹配的话,就说明这个activity是可以完成这个intent的意图的,可以将它激活。可以用setAction函数为intent对象指定一个动作,也可以用getAction读取Intent对象中的动作信息。
注意:一条<intent-filter>
元素至少应该包含一个<action>
,否则任何Intent请求都不能和该<intent-filter>
匹配。一个Intent只能指定一个action,但是一个Activity可以设置(监听、匹配)多个action(即intent-filter中可以设置多个action属性),这是两个不同的概念。 -
Category:用于指定当前动作(Action)执行的环境
即这个activity在哪个环境中才能被激活。不属于这个环境的,不能被激活。
如果该activity想要通过隐式intent方式激活,那么不能没有任何category设置,
至少包含一个android.intent.category.DEFAULT
一个Intent只能有一个Action,但是可以有多个Category.同一个Intent中的多个Category项彼此间是“与”的关系。也就是说一个组件需要支持全部的category项才能处理该请求。 -
Data:执行时要操作的数据,即执行动作的URI,不同的动作有不同的数据规格。
如果定义了Data,但intent却没有传进来指定类型的Data时,也不能激活该activity。
< data android:scheme="http" android:host=" www.baidu.com" />
常用的数据规格有:
实例:
效果:
(最后一个???有bug? 目前还不知道问题出在哪 ,找出来以后来更新)
结构:
- activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隐式调用Intent" />
<Button
android:id="@+id/btn_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开百度" />
<Button
android:id="@+id/btn_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拨打电话" />
<Button
android:id="@