Android Intent basic usage and object composition

Source: Internet
Author: User

Http://www.cnblogs.com/mengdd/archive/2013/03/18/2965839.htmlIntent Intent Basic use

Intent can understand the medium or messenger that communicates for different components .

Intent can start an activity, start a service, and initiate a broadcast broadcast.

The following table is the specific method:

Component Name

Method name

Activity

StartActivity ()

Startactivityforresult ()

Service

StartService ()

Bindservice ()

Broadcasts

Sendbroadcast ()

Sendorderedbroadcast ()

Sendstickybroadcast ()

1. Start activity:

To pass a intent object to the context.startactivity () or Activity.startactivityforresult () method, you can start an activity, Or make an existing activity to do something new.

(You can also pass this intent object to Activity.setresult () to return startActivityForResult() some information about the Activity that was called.) )

2. Start the service:

Passing the intent object to the Context.startservice () method initializes a service or passes a new instruction to an already existing service.

Similarly, passing the intent object to the Context.bindservice () method establishes a connection between the component where the call resides and the target service. If the service does not start running, it can initialize the service.

3. Launch of broadcast broadcast

You can pass a intent object to the following broadcast method to initiate a broadcast:

Context.sendbroadcast (), Context.sendorderedbroadcast (), or context.sendstickybroadcast (). The system will find the appropriate broadcast receiver to respond to this.

  

  Intent there are two ways to find a target component:

The first, an explicit invocation, is directly specified by component name;

The second, implicit invocation, does not explicitly specify the name of the target component, it is necessary to filter the filter by a certain condition.

The message system that initiates activity, service, and broadcast is not coincident, that is, a intent object that is to start the activity will never start a service.

Intent Object Composition

The intent object consists of the following six parts:

Component Name

Action

Data

Category

Extras

Flags

Component Name

Component name is the component name, which is the component name to process the intent object.

The component name object is encapsulated by the ComponentName class, and the component name contains the package name and class name , which is declared in the Androidmanifest.xml file.

The component name is obtained through the getcomponent () by SetComponent (), SetClass (), Setclassname () settings.

Note that component name is an option , and if set, then the intent object explicitly specifies the component to turn to, and if it is not set, the intent object needs to filter the lookup based on other information.

Action

An action is a string constant that intent the action to complete.

A number of action constants are defined within the intent class, including:

There are a number of predefined constants in the intent class, for some common actions, and others that are defined elsewhere in the Android API.

You can also define the action constants yourself, and the custom constants need to be prefixed with the package name of your app.

The action largely determines how other parts of intent are constructed, especially the data and extras domains. (as if the function name determines the value of the parameter and the return value.) So the name of the action should be as specific as possible, and they should be tightly combined with other domains in the intent.

Use Setaction () and Getaction () to set and read the Action property.

Data

The Data property is the URI and MIME type that performs the action, and different actions have different specifications.

For example, when the action is Action_edit, the data field is the uri;action of the document, the data field is Tel:uri, with the phone number to dial, and if the action is Action_view, the data field is http: Uri.

It is important to know the data type (MIME type) when matching intent and the components that can handle the data that the intent takes. For example, a component that displays an image should not be called to play an audio.

In many cases, data types, such as Content:uris, can be seen from URIs, indicating that the data is on the device, but is controlled by the content provider.

The data type can also be specified explicitly, such as the SetData () method specifies that the data is Uri,settype () specified as MIME type,setdataandtype () specifying that it is both a URI and a MIME type. Read the URI with GetData (), MIME type with GetType ().

Category

The category is a string that provides additional information about the kinds of components that can handle the intent object.

A intent object can contain any number of category description information.

Some category constants are also defined in the intent class:

The method corresponding to category has add addcategory (), remove removecategory (), and get all category GetCategories ().

Extras

The extra data passed to intent is defined as bundles, which are some key-value pairs. As if some actions correspond to a particular data URI, some actions correspond to a specific extras.

For example, action_timezone_changed intent object has a " time-zone " extra to confirm the new time zone;

  ACTION_HEADSET_PLUGThere is a " state " extra that the headset is plugged in and there is a " name " extra about the headset type;

If you want to design a show_color action, then the extra should contain the color value.

The intent object has a series of putxxx () functions to put into various data types, and a series of getxxx () functions are used to read the data.

In fact, data can be used as a bundle object using the Putextras () and Getextras () methods.

Flags

Various types of flag. Many are used to specify how the Android system initiates activity, and how to treat it when the activity is activated. All of these are defined in the intent class.

Android Intent basic usage and object composition

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.