Intent notes in Android

Source: Internet
Author: User

Intent is a runtime binding (Run-time binding) mechanism that connects two different components during a program's run. With intent, your program can express some kind of request or intention to Android, and Android will select the appropriate component to complete the request based on the intended content. For example, if an activity wants to open a Web browser to view the contents of a Web page, then the activity only needs to issue web_search_action to android,android according to intent's request content, Query the Intentfilter declared at each component registration, find the activity of the Web browser to browse the Web page.
Android's three basic components--activity,service and broadcast receiver--are all activated through the intent mechanism, and different types of components have different delivery intent methods:


1.1 To activate a new activity, or to have an existing activity do a new operation, you can call the context.startactivity () or the Activity.startactivityforresult () method.
1.2 To start a new service, or to pass a new instruction to an existing service, call the Context.startservice () method or call Context.bindservice () Method will call the context object of this method with the service binding.


The three methods of 1.3 context.sendbroadcast (), Context.sendorderbroadcast (), Context.sendstickbroadcast () can send broadcast Intent. Once sent, all broadcastreceiver that are registered and have matching intentfilter will be activated.
Once the intent is issued, Android will find exactly one or more matching activity,service or broadcastreceiver to respond. Therefore, there is no overlap between different types of intent messages, that is, broadcast intent messages are sent only to Broadcastreceiver and are never sent to the activity or service. Messages passed by StartActivity () will only be sent to activity, and intent delivered by StartService () will only be sent to the service.


The composition of the intent


To pass data between different activity, it is necessary to include the corresponding content in the intent, in general, the most basic of the data should include:


Action: Used to indicate what action to implement, such as Action_view, Action_edit, etc. Specifically, you can refer to the Android.content.intent class in Android sdk-> reference, where all the actions are defined in the constants.


Some of the commonly used action:


Action_call activity initiates a call.
Action_edit activity displays user-edited data.
Action_main activity starts as the first activity in a task
Action_sync activity synchronizes data on the phone with the data server.
Action_battery_low broadcast receiver Battery low warning.
Action_headset_plug broadcast receiver plug-in headset warning
ACTION_SCREEN_ON broadcast receiver screen brightens warning.
Action_timezone_changed broadcast receiver changes the time zone warning.


Data: A specific figure of fact, usually represented by a URI variable.

Category: A string that contains information about the kind of component that handles the intent. A intent object can have any category. The intent class defines many category constants.

The Addcategory () method adds a category to a intent object,
Removecategory Delete a category,
GetCategories () gets all the category intent.


Type: explicitly specifies the data type (MIME) of the intent (Multipurpose Internet Mail Extension, Multipurpose Internet Mail Extensions). For example, a component can display picture data without playing a sound file. In many cases, the data type can be found in URIs, such as content: A URI that begins with the content provider on the device. However, by setting this property, you can force an explicitly specified type to no longer be inferred.


There are 2 forms of MIME types:


1.1 Format of individual records: Vnd.android.cursor.item/vnd.yourcompanyname.contenttype, such as: content:// The MIME type of com.example.transportationprovider/trains/122 (URI for a train message) 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:// The MIME type of com.example.transportationprovider/trains (all train information) is Vnd.android.cursor.dir/vnd.example.rail


Component: Specifies the class name of the target component of the intent. Android usually finds a matching target component based on information about other attributes contained in the intent, such as action, Data/type, category. However, if the component property is specified, the component specified by it will be used directly, instead of performing the lookup procedure described above. When this property is specified, all other properties of intent are optional. For example:

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

StartActivity (IT);


Extras: Additional information, such as action_timezone_changed intent has a "time-zone" additional information to indicate the new time zone, and Action_headset_plug has a "state" Additional information to indicate whether the headset is plugged in or unplugged. Intent object has a series of put ... () and set ... () method 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 bundles. For example:


Transfer data with bundles Intent it = new Intent (Activity.Main.this, Activity2.class); Bundle Bundle=new Bundle (); Bundle.putstring ("name", "This was from mainactivity!"); It.putextras (bundle); StartActivity (IT); Get Data Bundle bundle=getintent (). Getextras (); String name=bundle.getstring ("name");


Analysis of Intent:

In the application, we can use intent in two ways:


1.1 Explicit Intent: Specifies the intent of the component property (called SetComponent (componentname) or SetClass (Context, Class). Notifies the app to launch the corresponding component by specifying a specific component class.

2.2 Implicit intent: No intent is specified for the Comonent property. These intent need to contain enough information so that the system can determine the components that meet this intent in all available components, based on this information.
For direct intent,android does not need to do parsing, because the target component is already very clear, Android needs to resolve those indirect intent, by parsing the intent map to the activity that can handle this intent, Service or broadcast Receiver.
Intent parsing mechanism
The intent parsing mechanism is mainly by looking up all the <intent-filter> in the Androidmanifest.xml and the intent defined therein, Through Packagemanager (note: Packagemanager is able to get the current device installed on the
Application package information) to find the component that can handle this intent. In this parsing process, Android is judged by the three attributes of the action, type, and category of intent, judging by the following:
1.1 If the action is specified by intent, the action must be included in the Intentfilter action list of the target component, otherwise it will not match;
1.2 If the intent does not provide a type, the system will get the data type from it. As with action, the data type list of the target component must contain the intent data type, otherwise it cannot be matched.
1.3 If the data in intent is not a content: type URI, and intent does not specify the type explicitly, the match will be based on scheme (such as http: or mailto:) of the data in intent. As above, the Intent scheme must be present in the scheme list of the target component.
1.4 If intent specifies one or more categories, they must all appear in the list of organized categories. For example, the intent contains two categories: Launcher_category and Alternative_category, and the parsed target component must contain at least these two categories.

Intent notes in Android

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.