Android Application Development Study Notes Intent

Source: Internet
Author: User

What is Intent? Let's take a look at the definition on the Android Official Website:

An intent is an abstractdescription of an operation to be performed med. it can be used with startActivity tolaunch an Activity, broadcastIntent tosend it to any interestedBroadcastReceiver components, and startService (Intent) or bindService (Intent, ServiceConnection, int) tocommunicate with a background Service.

An Intent provides afacility for faster Ming late runtime binding between the code in differentapplications. its most significant use is in the launching of activities, whereit can be thought of as the glue between activities. it is basically a passivedata structure holding an abstract description of an action to be performed med.

1. Composition of Intent attributes

Android starts corresponding components based on Intent. The component to be started depends on the attributes contained in Intent. Theoretically, Intent can contain the following attributes: Component name, Action, Data, Category, Extra, and Flags. The usage of these attributes is as follows:

 

1. Component name

The Component name attribute specifies the name of the Component used to process the Intent. It is a ComponentName class object. Componentname is an optional Intent attribute. If this attribute is set, the Intent will be sent to the Component specified by the Component name, which is called an explicit Intent ". If the Component name attribute is not set, Android uses other Intent attributes to determine the appropriate Sending target Component. This Intent is called "implicit Intent ".

The Component name attribute can be set through the setComponent (), setClass (), or setClassName () member functions of the Intent class, and can be read through the getComponent () member function. For example:

 
 

// Create a ComponentName object ComponentNamecomponentName = new ComponentName (FirstActivity. this, SecondActivity. class); Intentintent = new Intent (); // sets the Intent Component Attribute intent. setComponent (componentName); // start SecondActivity startActivity (intent); // create a ComponentName object ComponentNamecomponentName = new ComponentName (FirstActivity. this, SecondActivity. class); Intentintent = new Intent (); // sets the Intent Component Attribute intent. setComponent (componentName); // start SecondActivity startActivity (intent );


The above code can be simplified:

 

Intent intent = new Intent (FirstActivity. this, SecondActivity. class); // start SecondActivity startActivity (intent); Intent intent = new Intent (FirstActivity. this, SecondActivity. class); // start SecondActivity startActivity (intent );


 

2. Action

The Action attribute indicates the Action to be executed by the target component triggered by Intent. In Broadcast Intent, Action indicates that the Action to be reported to the target component has occurred. In the Intent class, many Action constants have been defined, and developers can also customize actions. The names of custom actions must be unique strings. Therefore, A good habit is to use the Java package-based naming method.

Action can be set through the setAction () member function of the Intent class and read through the getAction () member function. See the following example:

// Declare an Intent object Intentintent = new Intent (); // set the Action attribute. ACTION_DIAL indicates that the object is redirected to the intent on the dial-up interface. setAction (Intent. ACTION_DIAL); startActivity (intent); // declare an Intent object Intentintent = new Intent (); // set the Action attribute. ACTION_DIAL indicates that the interface jumps to intent. setAction (Intent. ACTION_DIAL); startActivity (intent );

3. Data

The Data attribute encapsulates the Data to be operated by the Action specified by the Action, including the URI and MIME Type of the Data to be operated by the Action. Different types of actions have different Data encapsulation. For example, if Action is ACTION_EDIT, Data should contain the URI used to edit the document. If Action is ACTION_CALL, Data should be the tel: URI containing the number. If the Action is ACTION_VIEW, the Data should be http: URI.

The image display component should not be used to play audio. Therefore, apart from the data URI, the data type is also very important. In many cases, the data type can be inferred from the URI, especially the content: URI, which indicates that the data exists on the device and is controlled by the ContentProvider. However, if necessary, in addition to the Data URI, Data can also explicitly contain its MIME type.

You can use the setData () member function of the Intent class to set the data URI, use the setType () member function to set the data MIME type, or use setDataAndType () the member function sets the data URI and MIME types at the same time. You can use the getData () member function to retrieve the data URI and use the getType () member function to read the MIME type.

 

4. Category

The Category attribute describes the Category of the target component used to process the Intent. An Intent can contain only one Action attribute, but multiple Category attributes. Similar to Action, the Intent class also predefines some Category constants.

You can use the addCategory () member function of the Intent class to add a Category attribute, delete a Category attribute using the removeCategory () member function, and use getCategory () the member function obtains all the Category attributes contained in the current object.

 

5. Extras

The Extras property encapsulates some additional information that exists as a key-value pair.

Just as some actions match a specific type of Data URI, some actions match a specific Extras. For example, if Action is ACTION_HEADSET_PLUG, Extras uses state to indicate whether the headset is inserted and name to indicate the type of the headset. For example, if Action is ACTION_TIMEZONE_CHANGED, Extras uses time-zone to represent the new zone.

The Intent class defines multiple putXXX () methods, such as putExtra (), used to set different types of Extra data. Multiple getXXX () methods are also defined, such as getDoubleExtra (), used to read Extra data. These methods are similar to Bundle classes. In fact, Extra can set and read Bundle through putExtras () and getExtras () methods.

 

6. Flags

The Flags attribute is mostly used to indicate how the Android system starts the Activity (such as the Task of which the Activity belongs) and how to treat the Activity (such as whether the Activity belongs to the recent Activity list ). All Flags are defined in the Intent class.

 

2. Intent Filter)

Components that can receive Intent processing (such as Activity, Service, BroadcastReceiver) must tell the Android system what kind of Intent it can respond to. This can be done through AndroidManifest. declare the Intent Filter Implementation in the xml file.

IntentFilter describes the ability of the component to respond to Intent, that is, the Action, Data, or Category attribute that the component can receive. For example, the IntentFilter of a Web browser program should declare that the Intent it wants to receive should have ACTION_WEB_SEARCH, and the related Data should be in the webpage url uri format.

1. Test Action

(1) Intent Filter should contain at least one Action test; otherwise, all Intent filters will be blocked.

(2) If the Intent does not define the Action attribute, the Intent will pass the test as long as the Intent Filter contains an Action test.

(3) An Intent can have only one Action attribute, but an Intent Filter can define multiple actions for testing. If the Intent Action attribute matches the Action test of an Intent Filter, pass the test.

 

2. Category Test

(1) One Intent can define multiple Category attributes, and the Intent Filter can also contain multiple Category tests, the Category test is successful only when all the Category attributes of Intent are matched in the Category test of Intent Filter.

(2) unnecessary Category test items in the Intent Filter will not cause the test to fail.

(3) In principle, no matter how the Intent Filter is defined, the Intent without the Category definition can always pass the Category test. However, it should be noted that Android sets a Category attribute for all the implicit Intent passed through the startActivity () method by default, that is, android. intent. category. DEFAULT (CATEGORY_DEFAULT). Therefore, the Activity that receives the implicit Intent must declare android in the filter. intent. category. DEFAULT (including android. intent. action. MAIN and android. intent. category. LAUNCHER sets an exception. They indicate that the Activity is started as a new task and displayed on the startup screen, including android. intent. category. DEFAULT or not ).

(4) An Intent Flilter that does not specify any Category test will only match the Intent that does not define the Category attribute.

 

3. Data Testing

In the Data test of Intent Filter, URI and Data type (MIME type) can be specified. URI can be divided into scheme, host, port and path independent parts: scheme: // host: port/path

Example: content: // com. liuhaoyu. project: 1000/folder/subfolder/config

Here, scheme is content, host is com. liuhaoyu. project, port is 1000, and path is folder/subfolder/config.

The host and port constitute URI authorization. If the host is not specified, the port is ignored.

These attributes are optional, but they are not completely independent from each other. If the authorization is valid, scheme must be specified. If the path is valid, scheme and authorization must be specified.

When the Data attribute of Intent is compared with the Data test of Intent Filter, it is only compared with the URI section actually mentioned in Intent Filter. For example, if the Intent Filter only specifies scheme, all URIs with the scheme can match the Intent Filter.

 

 

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.