1. Intent-Filter
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.HOME" />
- <category android:name="android.intent.category.DEFAULT"/>
- <category android:name="android.intent.category.MONKEY"/>
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里
当Intent通过startActivity被传递出后,Android系统会找到适合的activity去响应这个intent。
与此类似,startService,sendBroadcast之后都会由Android去寻找对应的程序。
一个Intent对象是一个信息包。它包含了要接收此Intent的组件需要的信息(例如需要的动作和动作需要的信息)和 android 系统需要的信息(要处理此Intent的组件的类别和怎样启动它)
主要用来指明Activity,Service, Broadcast Receiver可以响应哪些隐式intents。
action
一个字符串,代表要执行的动作。如果学过Structs2的同学应该不会陌生,所谓的action就是发送一个特定的请求,然后,由一个符合这个请求的activity响应。Intent类中定义了许多动作常量。如下:
常量 | 目标组件 | Action |
ACTION_CALL | activity | 初始化一个电话呼叫 |
ACTION_EDIT | activity | 显示用户要编辑的数据 |
ACTION_MAIN | activity | 将该Activity作为task的第一个Activity ,没有数据输入,也没有数据返回 |
ACTION_SYNC | activity | 在设备上同步服务器上的数据 |
ACTION_BATTERY_LOW | broadcast receiver | 电量不足的警告 |
ACTION_HEADSET_PLUG | broadcast receiver | 耳机插入设备,或者从设备中拔出 |
ACTION_SCREEN_ON | Broadcast receiver | 屏幕已经点亮 |
ACTION_TIMEZONE_CHANGED | Broadcast receiver | 时区设置改变 |
category
一个字符串, 包含了处理该Intent的组件的种类信息, 起着对action的补充说明作用。
一个Intent对象可以有任意多个 category。和action 一样, 在Intent class 中也定义了几个 category 常量。。 如下:
Constant | Meaning |
CATEGORY_BROWSABLE | 目标Activity可以使用浏览器显示数据 |
CATEGORY_GADGET | The activity can be embedded inside of another activity that hosts gadgets. 该activity可以被包含在另外一个装载小工具的activity中. |
CATEGORY_HOME | The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed. |
CATEGORY_LAUNCHER | 可以让一个activity出现在launcher |
CATEGORY_PREFERENCE | 该activity是一个选项面板 |
CATEGORY_DEAULT | 该activity是否可以接收到隐式Intents。说明除了程序入口点的filter不需要包含DEFAULT之外,其余所有的activity都要包含DEFAULT;因为如果那些activity不包含DEFAULT,将无法接收到主Activity的任何调用命令。 如SecondActivity中必须包含包名和DEFAULT的category. |
注意
A.任何一个需要隐式启动的Activity都必须要有这项:
category android:name="android.intent.category.DEFAULT"
例外情况是:
android.intent.category.MAIN和android.intent.category.LAUNCHER的filter中没有必要加入android.intent.category.DEFAULT,当然加入也没有问题
B. 当匹配不上任何Activity的话,会发生异常,跳出对话框:很抱歉...某某应用程序意外停止,请重试。
2. 普通的Activity
- <span style="font-size:18px;"> </span><intent-filter>
- <!-- The MAIN action describes a main entrypoint into an
- activity, without anyassociated data. -->
- <actionandroid:name="android.intent.action.MAIN" />
- <!-- This places this activity into the main app list. -->
- <categoryandroid:name="android.intent.category.LAUNCHER" />
- </intent-filter>
其中的MAIN表示这是该应用启动的第一个Activity, 而LAUNCHER表示此应用将被加载到Launcher的应用程序列表中.
3. Launcher的特殊处
Launcher的intent-filter是这样的,
- <action android:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.HOME"/>
- <categoryandroid:name="android.intent.category.DEFAULT"/>
- <categoryandroid:name="android.intent.category.MONKEY"/>
HOME表示该Activity可以作为Launcher, 即系统的第一个应用.
4. Launcher在系统中的调用过程