An Android application can contain zero or more
activities. If you want to navigate from one activity to another then
android provides you Intent class. This class is available
in android.content.Intent package. One of the most common uses for
Intents is to start new activities.
Intent
is a messaging object you can use to request an action
from another app component.
Although intents facilitate communication between components in several ways, there are three
fundamental use-cases:- To start an activity:
An
Activity
represents a single screen in an app. You can start a new instance of anActivity
by passing anIntent
tostartActivity()
. TheIntent
describes the activity to start and carries any necessary data.
If you want to receive a result from the activity when it finishes, callstartActivityForResult()
. Your activity receives the result as a separateIntent
object in your activity'sonActivityResult()
callback. For more information, see the Activities guide. - To start a service:
A
Service
is a component that performs operations in the background without a user interface. You can start a service to perform a one-time operation (such as download a file) by passing anIntent
tostartService()
. TheIntent
describes the service to start and carries any necessary data.
If the service is designed with a client-server interface, you can bind to the service from another component by passing anIntent
tobindService()
. For more information, see the Services guide. - To deliver a broadcast:
A broadcast is a message that any app can receive. The system delivers various
broadcasts for system events, such as when the system boots up or the device starts charging.
You can deliver a broadcast to other apps by passing an
Intent
tosendBroadcast()
,sendOrderedBroadcast()
, orsendStickyBroadcast()
.
Intent Type
There are two types of intents:
Explicit intents:- specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent to start a component in
your own app, because you know the class name of the activity or service you want to start. For
example, start a new activity in response to a user action or start a service to download
a file in the background.
Explicitly
starting an Activity
Intent intent = newIntent (this, SecondActivity.class);
startActivity(intent);
Here SecondActivity is the name of the target activity that you want to start.
Implicit intents :- do not name a specific component, but instead declare a general action
to perform, which allows a component from another app to handle it. For example, if you want to
show the user a location on a map, you can use an implicit intent to request that another capable
app show a specified location on a map.
When you create an explicit intent to start an activity or service, the system immediately
starts the app component specified in the
Intent
object.
Implicitly
starting an Activity
If you want to view a web page with the specified URL then you can use this procedure.
Intent i = newIntent(android.content.Intent.ACTION_VIEW,Uri.parse(“http://www.google.com”));
startActivity(i);
When you create an implicit intent, the Android system finds the appropriate component to start
by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the
device. If the intent matches an intent filter, the system starts that component and delivers it
the
Intent
object. If multiple intent filters are compatible, the system
displays a dialog so the user can pick which app to use.
An intent filter is an expression in an app's manifest file that
specifies the type of intents that the component
would like to receive. For instance, by declaring an intent filter for an activity,
you make it possible for other apps to directly start your activity with a certain kind of intent.
Likewise, if you do not declare any intent filters for an activity, then it can be started
only with an explicit intent.
This determination is based on Intent Filters. An Intent Filters specifies the types of Intent that an activity, service, or Broadcast Receiver can respond to. An Intent Filter declares the capabilities of a component. It specifies what an activity or service can do and what types of broadcasts a Receiver can handle. It allows the corresponding component to receive Intents of the declared type. Intent Filters are typically defined via the AndroidManifest.xml file. For Broadcast Receiver it is also possible to define them in coding. An Intent Filters is defined by its category, action and data filters. It can also contain additional metadata.
If a component does not define an Intent filter, it can only be called by explicit Intents.
Following are 2 ways to define a filter
Intent Filters
If an Intents is send to the Android system, it will determine suitable applications for this Intents. If several components have been registered for this type of Intents, Android offers the user the choice to open one of them.This determination is based on Intent Filters. An Intent Filters specifies the types of Intent that an activity, service, or Broadcast Receiver can respond to. An Intent Filter declares the capabilities of a component. It specifies what an activity or service can do and what types of broadcasts a Receiver can handle. It allows the corresponding component to receive Intents of the declared type. Intent Filters are typically defined via the AndroidManifest.xml file. For Broadcast Receiver it is also possible to define them in coding. An Intent Filters is defined by its category, action and data filters. It can also contain additional metadata.
If a component does not define an Intent filter, it can only be called by explicit Intents.
Following are 2 ways to define a filter
1.Manifest file
If you define the intent filter in the manifest, your application does not have to be running to react to the intents defined in it’s filter. Android registers the filter when your application gets installed.2.BroadCast Receiver
If you want your broadcast receiver to receive the intent only when your application is running. Then you should define your intent filter during run time.Building an Intent
An
Intent
object carries information that the Android system uses
to determine which component to start (such as the exact component name or component
category that should receive the intent), plus information that the recipient component uses in
order to properly perform the action (such as the action to take and the data to act upon).
The primary information contained in an
Intent
is the following:- Component name
- The name of the component to start. This is optional, but it's the critical piece of information that makes an intent explicit, meaning that the intent should be delivered only to the app component defined by the component name. Without a component name, the intent is implicit and the system decides which component should receive the intent based on the other intent information (such as the action, data, and category—described below). So if you need to start a specific component in your app, you should specify the component name.
- Action
- A string that specifies the generic action to perform (such as view or pick).
In the case of a broadcast intent, this is the action that took place and is being reported.
The action largely determines how the rest of the intent is structured—particularly
what is contained in the data and extras.
You can specify your own actions for use by intents within your app (or for use by other apps to invoke components in your app), but you should usually use action constants defined by theIntent
class or other framework classes. Here are some common actions for starting an activity:
ACTION_VIEW
- Use this action in an intent with
startActivity()
when you have some information that an activity can show to the user, such as a photo to view in a gallery app, or an address to view in a map app. ACTION_SEND
- Also known as the "share" intent, you should use this in an intent with
startActivity()
when you have some data that the user can share through another app, such as an email app or social sharing app.
Intent
class reference for more constants that define generic actions. Other actions are defined elsewhere in the Android framework, such as inSettings
for actions that open specific screens in the system's Settings app.
You can specify the action for an intent withsetAction()
or with anIntent
constructor.
If you define your own actions, be sure to include your app's package name as a prefix. - Data
- The URI (a
Uri
object) that references the data to be acted on and/or the MIME type of that data. The type of data supplied is generally dictated by the intent's action. For example, if the action isACTION_EDIT
, the data should contain the URI of the document to edit. When creating an intent, it's often important to specify the type of data (its MIME type) in addition to its URI. For example, an activity that's able to display images probably won't be able to play an audio file, even though the URI formats could be similar. So specifying the MIME type of your data helps the Android system find the best component to receive your intent. However, the MIME type can sometimes be inferred from the URI—particularly when the data is acontent:
URI, which indicates the data is located on the device and controlled by aContentProvider
, which makes the data MIME type visible to the system.
To set only the data URI, callsetData()
. To set only the MIME type, callsetType()
. If necessary, you can set both explicitly withsetDataAndType()
.
Caution: If you want to set both the URI and MIME type, do not callsetData()
andsetType()
because they each nullify the value of the other. Always usesetDataAndType()
to set both URI and MIME type. - 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, but most intents do not require a category.
Here are some common categories:
CATEGORY_BROWSABLE
- The target activity allows itself to be started by a web browser to display data referenced by a link—such as an image or an e-mail message.
CATEGORY_LAUNCHER
- The activity is the initial activity of a task and is listed in the system's application launcher.
Intent
class description for the full list of categories.
You can specify a category withaddCategory()
.
These properties listed above (component name, action, data, and category) represent the
defining characteristics of an intent. By reading these properties, the Android system
is able to resolve which app component it should start.
However, an intent can carry additional information that does not affect
how it is resolved to an app component. An intent can also supply:
- Extras
- Key-value pairs that carry additional information required to accomplish the requested action.
Just as some actions use particular kinds of data URIs, some actions also use particular extras.
You can add extra data with various
putExtra()
methods, each accepting two parameters: the key name and the value. You can also create aBundle
object with all the extra data, then insert theBundle
in theIntent
withputExtras()
.
For example, when creating an intent to send an email withACTION_SEND
, you can specify the "to" recipient with theEXTRA_EMAIL
key, and specify the "subject" with theEXTRA_SUBJECT
key.
TheIntent
class specifies manyEXTRA_*
constants for standardized data types. If you need to declare your own extra keys (for intents that your app receives), be sure to include your app's package name as a prefix.
- Flags
- Flags defined in the
Intent
class that function as metadata for the intent. The flags may instruct the Android system how to launch an activity (for example, which task the activity should belong to) and how to treat it after it's launched (for example, whether it belongs in the list of recent activities). For more information, see thesetFlags()
method.
No comments:
Post a Comment