原文:Class Overview
An intent is an abstract description of an operation to be performed. It can be used with startActivity
to launch an Activity
, broadcastIntent
to send it to any interestedBroadcastReceiver
components, and startService(Intent)
or bindService(Intent, ServiceConnection, int)
to communicate with a background Service
.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
翻译:intent是对将要被执行的动作的一种抽象描述,它能和startAcitivity一起被使用用来开始一个Activity,能和broadcastIntent一起使用来把它传送给任何一个有益的BroadcastReceiver组成部分,也能和startService(Intent)或者bindService(Intent,ServiceConnection,int)一起使用来和一个背景Service交流。
intent在不同的应用的代码中提供了一个能表现出最近运行时间的设备。它最重要的用途是在开展各种acitivities中,在此之中它可以被当做是activities之中的联结物。它基本是个被动的数据结构,它拥有对一个将被执行动作的抽象描述。
原文:
Intents and Intent Filters
In this document
Key classes
Three of the core components of an application — activities, services, and broadcast receivers — are activated through messages, called intents. Intent messaging is a facility for late run-time binding between components in the same or different applications. The intent itself, an Intent
object, is a passive data structure holding an abstract description of an operation to be performed — or, often in the case of broadcasts, a description of something that has happened and is being announced. There are separate mechanisms for delivering intents to each type of component:
- An Intent object is passed to
orContext.startActivity()
to launch an activity or get an existing activity to do something new. (It can also be passed toActivity.startActivityForResult()
to return information to the activity that calledActivity.setResult()
startActivityForResult()
.) -
An Intent object is passed to
to initiate a service or deliver new instructions to an ongoing service. Similarly, an intent can be passed toContext.startService()
to establish a connection between the calling component and a target service. It can optionally initiate the service if it's not already running.Context.bindService()
-
Intent objects passed to any of the broadcast methods (such as
,Context.sendBroadcast()
, orContext.sendOrderedBroadcast()
) are delivered to all interested broadcast receivers. Many kinds of broadcasts originate in system code.Context.sendStickyBroadcast()
In each case, the Android system finds the appropriate activity, service, or set of broadcast receivers to respond to the intent, instantiating them if necessary. There is no overlap within these messaging systems: Broadcast intents are delivered only to broadcast receivers, never to activities or services. An intent passed to startActivity()
is delivered only to an activity, never to a service or broadcast receiver, and so on.
This document begins with a description of Intent objects. It then describes the rules Android uses to map intents to components — how it resolves which component should receive an intent message. For intents that don't explicitly name a target component, this process involves testing the Intent object against intent filters associated with potential targets.
Intent Objects
An Intent
object is a bundle of information. It contains information of interest to the component that receives the intent (such as the action to be taken and the data to act on) plus information of interest to the Android system (such as the category of component that should handle the intent and instructions on how to launch a target activity). Principally, it can contain the following:
-
Component name
-
The name of the component that should handle the intent. This field is a
ComponentName
object — a combination of the fully qualified class name of the target component (for example "com.example.project.app.FreneticActivity
") and the package name set in the manifest file of the application where the component resides (for example, "com.example.project
"). The package part of the component name and the package name set in the manifest do not necessarily have to match.The component name is optional. If it is set, the Intent object is delivered to an instance of the designated class. If it is not set, Android uses other information in the Intent object to locate a suitable target — see Intent Resolution, later in this document.