Intent notes in Android

Source: Internet
Author: User

Intent notes in Android
Intent is a run-time binding mechanism, which can be used to connect two different components while the program is running. With Intent, your program can express a request or willingness to Android. Android selects appropriate components based on the content you want to complete the request. For example, if an Activity wants to open a webpage browser to view the content of a webpage, the Activity only needs to send WEB_SEARCH_ACTION to Android, and Android will send the Intent request content according to the content, query the IntentFilter declared when each component is registered, and find the Activity of the web browser to browse the Web page.
The three basic components of Android-Activity, Service, and Broadcast Receiver-are activated through the Intent mechanism. Different types of components have different Intent transmission methods:


1.1 to activate a new Activity or perform new operations on an existing Activity, you can call the Context. startActivity () or Activity. startActivityForResult () method.
1.2 to start a new Service or send a new command to an existing Service, call Context. startService () method or call Context. the bindService () method binds the context object that calls this method to the Service.


1.3 The Context. sendBroadcast (), Context. sendOrderBroadcast (), and Context. sendStickBroadcast () methods can send Broadcast Intent. After sending the message, all registered BroadcastReceiver with the IntentFilter matching it will be activated.
Once the Intent is sent, Android will accurately find one or more matching activities, services or BroadcastReceiver for response. Therefore, different types of Intent messages do not overlap. That is, Broadcast Intent messages are sent only to BroadcastReceiver and never to Activity or Service. Messages transmitted by startActivity () are only sent to Activity, and Intent transmitted by startService () is only sent to Service.


Intent Composition


To transmit data between different activities, you must include the corresponding content in intent. Generally, the most basic data should include:


Action: Specifies the Action to be implemented, such as ACTION_VIEW and ACTION_EDIT. For details, refer to the android SDK-> reference Android. content. intent class, which defines all actions in constants.


Some common actions:


ACTION_CALL activity starts a call.
ACTION_EDIT activity displays edited data.
ACTION_MAIN activity is started as the first Activity in the Task.
ACTION_SYNC activity synchronizes data on mobile phones and data servers.
ACTION_BATTERY_LOW broadcast comment er indicates that the battery is too low.
ACTION_HEADSET_PLUG broadcast receiver er plug-in headset warning
The screen of ACTION_SCREEN_ON broadcast receiver er is highlighted with a warning.
ACTION_TIMEZONE_CHANGED broadcast receiver Time Zone change warning.


Data: Specific Data of facts, usually expressed by a Uri variable.

Category: a string that contains information about the types of components that process the intent. An intent object can have any category. The intent class defines many category constants.

AddCategory () method adds a category to an intent object,
RemoveCategory deletes a category,
GetCategories () to get all intent category.


Type: explicitly specifies the Intent data Type (MIME) (multi-purpose Internet Mail Extension, Multipurpose Internet Mail Extensions ). For example, a component can display image data but cannot play audio files. In many cases, the data type can be found in the URI. For example, the URI starting with content: indicates that the data is provided by the content provider on the device. However, by setting this attribute, You can forcibly use the explicitly specified type without derivation.


The MIME type has two forms:


1.1 format of a single record: vnd. android. cursor. item/vnd. yourcompanyname. contenttype, such as content: // com. example. the MIME type of transportationprovider/trains/122 (a train information uri) is vnd. android. cursor. item/vnd. example. rail

Format of more than 1.2 records: vnd. android. cursor. dir/vnd. yourcompanyname. contenttype, such as content: // com. example. the MIME type of transportationprovider/trains (all train information) is vnd. android. cursor. dir/vnd. example. rail


Component: Specifies the Class Name of the Intent's target component. Generally, Android searches for other attributes in Intent, such as action, data/type, and category, and finds a matched target component. However, if this attribute of component is specified, the component specified by component will be used directly without executing the above search process. After this attribute is specified, all other Intent attributes are optional. For example:

Intent it = new Intent (Activity. Main. this, Activity2.class); startActivity (it );

StartActivity (it );


Extras: Additional information. For example, the intent of ACTION_TIMEZONE_CHANGED has a "time-zone" additional information to indicate the new time zone, ACTION_HEADSET_PLUG has an additional "state" to indicate whether the headset is inserted or pulled out. Intent objects have a series of put... () and set... () Methods to set and obtain additional information. These methods are similar to Bundle objects. In fact, additional information can be read and written using putExtras () and getExtras () as Bundle. For example:


// Use Bundle to transmit data Intent it = new Intent (Activity. main. this, Activity2.class); Bundle bundle = new Bundle (); bundle. putString ("name", "This is from MainActivity! "); It. putExtras (bundle); startActivity (it); // get data Bundle bundle = getIntent (). getExtras (); String name = bundle. getString ("name ");


Intent parsing:

In applications, we can use Intent in two forms:


1.1 explicit Intent: Specifies the Intent of the component Attribute (call setComponent (ComponentName) or setClass (Context, Class ). Notify the application to start the corresponding component by specifying a specific component class.

2.2 implicit Intent: the Intent of the comonent attribute is not specified. The Intent must contain sufficient information so that the system can determine the components that meet the Intent among all available components based on the information.
For a direct Intent, Android does not need to parse it, because the target component is clear, and Android needs to parse the indirect Intent, the Intent is mapped to the Activity, Service, or Broadcast Receiver that can process the Intent through parsing.
Intent parsing Mechanism
The Intent parsing mechanism is mainly used to find all And the Intent defined in the PackageManager (Note: PackageManager can obtain
Application package information) to find the component that can process the Intent. In this parsing process, Android uses the Intent action, type, and category attributes to determine the attributes. The judgment method is as follows:
1.1 If Intent indicates that action is set, the action list of the IntentFilter of the target component must contain this action; otherwise, the action cannot match;
1.2 If the Intent does not provide a type, the system will obtain the data type from the data. Like action, the Data Type list of the target component must contain the Intent data type. Otherwise, the data type does not match.
1.3 if the data in the Intent is not the content: type URI and the Intent does not explicitly specify the type, it will match according to the scheme (such as http: or mailto :) of the data in the Intent. Similarly, the scheme of Intent must appear in the scheme list of the target component.
1.4 If Intent specifies one or more category categories, these categories must all appear in the Set category list. For example, Intent contains two categories: LAUNCHER_CATEGORY and ALTERNATIVE_CATEGORY. The parsed target component must contain at least these two categories.

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.