Summary Intnet and IntentFilter are used for delivering structured messages between different application components—allowing components to initiate other components and return results.
Classes
Intent What's Intent ========================== An intent is a
bundle of information.
The core components of an application (activities, services, and broadcast
receivers) are activated through structured messages, called intents.
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.
Intent are used to activate activity, service, broadcastreceiver:
component name The name of the component that should handle the intent. This field is a ComponentName object. 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.
action A string naming the action to be performed — or, in the case of broadcast
intents, the action that took place and is being reported. The action largely determines how the rest of the intent is structured
— particularly the and
fields —
much as a method name determines a set of arguments and a return value.
For this reason, it's a good idea to use action names that are
as specific as possible, and to couple them tightly to the other fields of
the intent (such as data, and extras).
data (both URI and data type) The URI of the data to be acted on and the MIME type of that data. Different
actions are paired with different kinds of data specifications.
category A string containing additional information about the kind of component
that should handle the intent. Any number of category descriptions can be
placed in an Intent object.
extras Key-value pairs for additional information that should be delivered to the
component handling the intent.
flags Instruct the Android system how to launch an
activity and how to treat
it after it's launched
Intent Resolution ========================== Intents can be divided into two groups:
Explicit intents designate the target component by its
name. Since component names would generally not be known to
developers of other applications, explicit intents are typically used
for application-internal messages — such as an activity starting
a subordinate service or launching a sister activity.
Implicit intents do not name a target (the field for
the component name is blank). Implicit intents are often used to
activate components in other applications.
For explicit intents, Android directly delivers an explicit intent to an instance of the designated target class. Nothing in the Intent object other than the component
name matters for determining which component should get the intent.
While for implicit intents, the Android system will find the best component (or
components) to handle the intent — a single activity or service to
perform the requested action or the set of broadcast receivers to respond
to the broadcast announcement. It does so by comparing the contents of
the Intent object to intent filters --- structures associated with
components that can potentially receive intents.
Only three aspects of an Intent object are consulted when the object is tested against an intent filter: - action - data (both URI and data type: MIME) - category
To be delivered to the component that owns the filter, it must pass all three tests.
If it fails even one of them, the Android system won't deliver it to the
component. An intent filter can define multipe action(or data/category), while an intent only can specify single action(or data/category), to pass test, the action(or data/category) specified in the Intent object must match
one of the actions/data/catetory listed in the filter.. The extras and flags play no part in resolving which component receives an intent.
If a component does not have any intent filters,
it can receive only explicit intents. A component with filters can
receive both explicit and implicit intents.
IntentFilter Inform the system which implicit intents they can handle.
Activities,
services, and broadcast receivers can have one or more intent filters.
Each filter describes a capability of the component, a set of intents that
the component is willing to receive.
For example, the NoteEditor activity of the sample
Note Pad application has two filters — one for starting up with a
specific note that the user can view or edit, and another for starting
with a new, blank note that the user can fill in and save.
Sample: a) register broadcast receiver IntentFilter filter = new IntentFilter(); filter.setAction(xxx); filter.addDataSchema("file"); context.registerReceiver(receiver, filter); b) When to send broadcast, if you don't set data path, the receiver can't receive it; Intent intent = new Intent(); intent.setAction(xxx); //intent.setDataAndType("file:///sdcard/a.txt", "images/png");
ie2: launch an implicit Activity of other application Intent intent = new Intent(); intent.setAction(xxx): intent.setCategory(xxx); intent.setCategory(Intent.CATEGORY_DEFAULT); //Necessary startActivity(intent);
An intent filter is an instance of the class.
However, since the Android system must know about the capabilities of a component
before it can launch that component, intent filters are generally NOT set up in
Java code, but in the application's manifest file (AndroidManifest.xml) as
elements. (The one exception would be filters for
broadcast receivers that are registered dynamically by calling ; they are directly created as
IntentFilter objects.)
Structured description of Intent values to be matched. An IntentFilter can
match against actions, categories, and data (either via its type, scheme,
and/or path) in an Intent.
Intent characteristics to filter on:
action A filter must
contain at least one element, or it
will block all intents.
The standard 'android.intent.action.MAIN' action is an entry point that does not require
any other information in the Inten
catagory 'android.intent.category.LAUNCHER' category says that this entry point should be
listed in the application launcher.
'android.intent.category.DEFAULT' category is required for all
filters — except for those with the MAIN action and
LAUNCHER category. (Intent filters are not consulted for
explicit intents.),
because the Context.startActivity() and Activity.startActivityForResult() methods treat all intents as if they contained the DEFAULT category (that means you need not to set category to the Intent object) — with just two exceptions: - Intents that explicitly name the target activity - Intents consisting of the MAIN action and LAUNCHER category
data The data test compares both the URI and the data type in the Intent object to a URI
and data type specified in the filter.
When the URI in an Intent object is compared to a URI specification in a filter,
it's compared only to the parts of the URI actually mentioned in the filter.
For example, if a filter specifies only a scheme, all URIs with that scheme match
the filter. If a filter specifies a scheme and an authority but no path, all URIs
with the same scheme and authority match, regardless of their paths. If a filter
specifies a scheme, an authority, and a path, only URIs with the same scheme,
authority, and path match. However, a path specification in the filter can
contain wildcards to require only a partial match of the path.
type Both the Intent object and
the filter can use a "*" wildcard for the subtype field — for example,
"text/*" or "audio/*" — indicating any subtype matches.
schema
authority
path
xxx
If an intent can pass through the filters of more than one (KeyWorkd: several, multiple) activity or service,
the user may be asked which component to activate. (you can use this method to override system applications, please see the following samples) An exception is raised if
no target can be found.
Override system apps: a) Override Dailer
PendingIntent A description of an Intent and target action to perform at a later time.
xxx
Start Component (Activity, Service, Broadcast Receiver) with Intent When the system starts a component of other application, it starts the process for that application (if it's not
already running) and instantiates the classes needed for the component.