Android ---- Intent

Source: Internet
Author: User

Android ---- Intent

1. Functions of Intent for Android applications

There are two functions:

① Encapsulate the "intention" of an Android Application to start a component"

② As a medium for communication between application components, encapsulate the data to be exchanged into Bundle objects, and then use Intent to carry Bundle objects

2. Explanation of Intent objects 2.1 use Intent to start system components

Start Activity: startActivity (Intent intent)

StartActivityForResult (Intent intent, int requestCode)

Start Service: ComponentName startService (Intent intent)

Boolean bindService (Intent service, ServiceConnection conm, int flags)

Start BroadcastReceiver: sendBroadcast (Intent intent)

SendBroadcast (Intent intent, String assumermission) etc

2.2 Intent attributes and intent-filter configuration

Intent indicates the intention of the Android app to start an app. The Android app starts a specified component based on Intent,

You need to determine which component to start based on the attribute values you do not understand.

2.2.1 Component attributes

First, introduce the concept of a component, including Service, Activity, Content ),

Intent (Intent), BroadcastRece (broadcast), Notification (Notification)

The Component Attribute of Intent accepts a value called ComponentName. The ComponentName constructors include:

Component (String pkg) // create all components corresponding to the cls class in the package where the pkg is located

Component (String pkg, String cls) // create the Component corresponding to the cls class in the package corresponding to pkg

Component (String pkg, Class Cls) // create the components corresponding to the cls class under the package corresponding to pkg

All of them determine a component based on the package and class name and then start it.

You can also use the setClass (Context package, Class Cls), setClassName (Context packageContext, String className)

SetClassName (Strong packageName, String className) to specify the component to be started by the Intent

This explicitly specifies which component to start is called explicit Intent, and another component that starts the kimono rule according to the rule is called implicit Intent.

For example

HelloWorld1.javaIntent intent = new Intent (); ComponentName component = new ComponentName (HelloWorld1.this, HelloWorld2.class); intent. setComponent (component); startActivity (intent); HelloWorld2.javaComponentName component = getIntent (). getComponent (); // you can use Intent to obtain the value of Component.

2.2.2 Action, Category attribute and intent-filter configuration

The Action and Category attributes of Intent are common strings. Action indicates an abstract Action to be completed by the Intent, while Category is used

Add additional nearby category information for the Action. Generally, Action and Category attributes are used in combination.

Configuration information of an Activity or other components The value of Action and Category. Usually 0 ~ N child elements

0 ~ N Child element and 0 ~ 1Child element. This is the condition for a pretty girl to find a friend.

When the Action and Category set by Intent all meet the Action and Category of the component, the component will be opened, which is equivalent to a boy who is completely satisfied with the information found by her sister.

The conditions are all together.

Generally, Intent only specifies one Action, but multiple Category can be specified.

2.2.3 specify Action and Category to call system Activity

Click to open the corresponding table of Action and Category.

2.2.4 Data and Type attributes and intent-filter configuration

The Data property is usually used to provide operation Data to the Action property. The Data property accepts a uri object, which is usually represented by a string in the following format:

      content://com.android.contacts/contacts/1      tel:123
The uri string must meet the following format:

Scheme: // host: port/path

Content is part of scheme, com. android. contacts is part of host, port is omitted,/contacts/1 is part of path

The Type attribute is used to specify the MIME Type corresponding to the specified uri of the Data. This MIME Type can be any custom MIME Type,

Only strings in the abc/xyz format can be used.

The Type and Data attributes are sometimes overwritten. For example, if the Intent sets the Data attribute first and then the Type attribute, Tpye overwrites the Data

And vice versa. The solution is to call the setDataAndType () method.

In the AndroidMainifest. xml fileElement to set the Type and Data attribute values

The element supports the following attributes:

MimeType: Used to declare the Type attribute of the Intent that the component matches.

Scheme: Used to declare the scheme part of the Data attribute of the Intent that the component matches.

Data: Declares the host part of the Data attribute of the Intent that the component matches.

Host: Declares the host part of the Data attribute of the Intent that the component matches.

Port: Declares the port part of the Data attribute of the Intent that the component matches.

Path: Declares the path part of the Data attribute of the Intent that the component matches.

PathPrefix: Used to declare the path prefix of the Data Attribute of the Intent that the component matches.

PathPattern: Specifies the path string template used to declare the Data attribute of the Intent that the component matches.

When starting the component, the Type must be completely consistent, but the Intent data value is only set to a part, then you only need to meet the set part to start.

2.2.5 Extra attributes

The Extra attribute of Intent is usually used for data exchange among multiple actions. The Extra attribute value of Intent should be a Bnudle object, Bundle

An object is a Map object, which can store multiple pairs of key-value values.

2.2.6 Flag attributes

The Flag attribute is used to add some additional control flagpole for the Intent. You can call the addFlags () method to add a control flagpole for the Intent.

Click the link to open the table corresponding to the Flag attribute.

2.3 use Intent to create a Tab page

Content on different Tab pages is an Activity

TabHost tabHost = getTabHost (); TabHost. addTab (tabHost. newTabSpec ("tab1 "). setIndicator ("answered "). setContent (new Intent (this, HelloWorld1.class); TabHost. addTab (tabHost. newTabSpec ("tab2 "). setIndicator ("missed "). setContent (new Intent (this, HelloWorld2.class); TabHost. addTab (tabHost. newTabSpec ("tab3 "). setIndicator ("outgoing call "). setContent (new Intent (this, HelloWorld3.class )));


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.