Intents and Intent Filters

Intentとは

Intent 是一个消息传递对象,您可以使用它从其他应用组件请求操作。在三个核心程序组件「activitiesservicesbroadcast receivers」之间传递数据。尽管 Intent 可以通过多种方式促进组件之间的通信,但其基本用例主要包括以下三个:

種類 起動方法 COMMENT
Activity Context.startActivity(Intent intent)
Activity.startActivityForResult() RESULT:Activity.setResult()
Service Context.startService()
Context.bindService()
BroadcastReceiver Context.sendBroadcast()
Context.sendOrderedBroadcast()
Context.sendStickyBroadcast()

Component name

  • Component属性 明确指定Intent的目标组件的类名称
  • 通常 Android框架会根据Intents 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个匹配的目标组件。但是,如果 Component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intents的其它所有属性都是可选的。
1
2
3
4
5
6
7
8
private static final String PKJ = "com.example.sampleactivity";
private static final String CLS = "com.example.sampleactivity.Activity2";

Intent intent = new Intent(Intent.ACTION_MAIN);
ComponentName cn = new ComponentName(PKJ, CLS);
intent.setComponent(cn);
intent.putExtra("input", "I'm from intent apk");
startActivity(intent);

Action

对执行动作的描述:操作(Action)
系统自定义了很多Action,ACTION_MAIN ,我们最熟悉的一个。
 “android.intent.action.MAIN”
,这个值我们在每个AndroidManifest.xml文档中都可以看到。它标记当前Activity作为一个程序的入口。
自己也可以定义自己的Action

Constant Target component Action
ACTION_CALL activity Initiate a phone call.
ACTION_EDIT activity Display data for the user to edit.
ACTION_MAIN activity Start up as the initial activity of a task, with no data input and no returned output.
ACTION_SYNC activity Synchronize data on a server with data on the mobile device.
ACTION_BATTERY_LOW broadcast receiver A warning that the battery is low.
ACTION_HEADSET_PLUG broadcast receiver A headset has been plugged into the device, or unplugged from it.
ACTION_SCREEN_ON broadcast receiver The screen has been turned on.
ACTION_TIMEZONE_CHANGED broadcast receiver The setting for the time zone has changed.

数据

引用待操作数据和/或该数据 MIME 类型的 URIUri 对象)。提供的数据类型通常由 Intent 的操作决定。例如,如果操作是 ACTION_EDIT,则数据应包含待编辑文档的 URI。 创建 Intent 时,除了指定 URI 以外,指定数据类型(其 MIME 类型)往往也很重要。例如,能够显示图像的 Activity 可能无法播放音频文件,即便 URI 格式十分类似时也是如此。因此,指定数据的 MIME 类型有助于 Android 系统找到接收 Intent 的最佳组件。但有时,MIME 类型可以从 URI 中推断得出,特别当数据是 content: URI 时尤其如此。这表明数据位于设备中,且由 ContentProvider 控制,这使得数据 MIME 类型对系统可见。

要仅设置数据 URI,请调用 setData()。 要仅设置 MIME 类型,请调用 setType()。如有必要,您可以使用 setDataAndType() 同时显式设置二者。

注意

若要同时设置 URIMIME 类型,请勿调用 setData()setType(),因为它们会互相抵消彼此的值。请始终使用 setDataAndType() 同时设置 URIMIME 类型。

类别

一个包含应处理 Intent 组件类型的附加信息的字符串。 您可以将任意数量的类别描述放入一个 Intent 中,但大多数 Intent 均不需要类别。 以下是一些常见类别:

  • CATEGORY_BROWSABLE 目标 Activity 允许本身通过网络浏览器启动,以显示链接引用的数据,如图像或电子邮件。
  • CATEGORY_LAUNCHER 该 Activity 是任务的初始 Activity,在系统的应用启动器中列出。

有关类别的完整列表,请参阅 Intent 类描述。 您可以使用 addCategory() 指定类别。

|Constant|Meaning| |CATEGORY_BROWSABLE|The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.| |CATEGORY_GADGET|The activity can be embedded inside of another activity that hosts gadgets.| |CATEGORY_HOME|The activity displays the home screen, the first screen the user sees when the device is turned on or when the Home button is pressed.| |CATEGORY_LAUNCHER|The activity can be the initial activity of a task and is listed in the top-level application launcher.| |CATEGORY_PREFERENCE|The target activity is a preference panel.|

Extras

携带完成请求操作所需的附加信息的键值对。正如某些操作使用特定类型的数据 URI 一样,有些操作也使用特定的 extra。
您可以使用各种 putExtra() 方法添加 extra 数据,每种方法均接受两个参数:键名和值。您还可以创建一个包含所有 extra 数据的 Bundle 对象,然后使用 putExtras() 将Bundle 插入 Intent 中。

例如,使用 ACTION_SEND 创建用于发送电子邮件的 Intent 时,可以使用 EXTRA_EMAIL 键指定“目标”收件人,并使用 EXTRA_SUBJECT 键指定“主题”。

Intent 类将为标准化的数据类型指定多个 EXTRA_* 常量。如需声明自己的 extra 键(对于应用接收的 Intent),请确保将应用的软件包名称作为前缀。 例如:

1
static final String EXTRA_GIGAWATTS = "com.example.EXTRA_GIGAWATTS";

标志

在 Intent 类中定义的、充当 Intent 元数据的标志。 标志可以指示 Android 系统如何启动 Activity(例如,Activity 应属于哪个任务),以及启动之后如何处理(例如,它是否属于最近的 Activity 列表)。 如需了解详细信息,请参阅 setFlags() 方法。

参照: intents-filters