Androge Feature Article-intent

Source: Internet
Author: User

Android three basic components--activity,service and broadcast receiver--are activated through the intent mechanism, different types of components have different delivery intent way, We often use intent, but if we do not seriously analyze intent, we often foggy.

So what is intent, intent, is what you have to do through intent. is an abstract description of the action that will be performed by intent to assist in the completion of communication between the various components of Android. For example, invoke the activity instantiation object's startactivity () to initiate an activity, or broadcaseintent () to all interested broadcasereceiver, or by StartService ()/bindservice () to start a backend service. As can be seen, intent is primarily used to initiate activity or service (and carry parameter information that needs to be passed), intent to understand the binder between activity.

in short, intent has the ability to activate components and carry data! The intent method of the three main components:
1.1 To activate a new activity, or to have an existing activity do a new operation, you can call 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 the Context.bindservice () method to invoke the top and bottom of this method . Intent the form object to the service binding.

1.3 context.sendbroadcast (), Context.sendorderbroadcast (), Context.sendstickbroadcast () These three methods can send broadcast INT Ent. 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.

Intent form

(1). Display (Explicit Intents)         explicitly specifying the intent of the component name as an explicit intent, specifies that the intent should be passed to that component. By following the code, you can create a materialized object and set the parameter information that needs to be passed. Because the specific component object is explicitly specified, it is not necessary to set other intent filtering objects for intent. [Java]  // 1. Creating Intent instantiation objects    intent Intent = new Intent ();  intent.setclass (Context packagecontext, class<?> CLS);          //Internal call SetComponent (componentname)  intent.setclassname (Context Packagecontext , String ClassName); Internal call SetComponent (componentname)  intent.setclassname (string PackageName, string className);    /Internal call setcomponent (componentname), you can activate the external application    intent.setcomponent (new ComponentName ( This, class<?> CLS));  intent.setcomponent (New ComponentName (This, "Package Name")); &NBSP;2). Implicit intent (implicit Intents) does not explicitly specify the intent of the component name as implicit intent, the system will match the most appropriate component based on the action (action), category, data URI, etc. set in the implicit intent. 1). Actionthe general action to be performed, such as Action_view, Action_edit, Action_main, including Android system specified and custom [Java] &n Bsp;inteNt.setaction ("Com.baidu.action.TEST");  [html] View plaincopy<action android:name= "Com.baidu.action.TEST"/>  2). dataexpressed as a Uri, the Data to operate on, such as a person record in the contacts database. The action of the system comes with a simple example action Data (Uri)Contentaction_viewcontent://contacts/people/1display information about the person whose identifier is "1". Action_viewtel:123display the Phone Dialer with the given number filled in. Action_dialtel:123display the Phone Dialer with the given number filled in.  custom data matching [Java]  intent.setdata ( Uri.parse ("Baidu://www.baidu.com/news"));  [html]  <!--Android:path content strings need to be/start with-- <data android:scheme= "Baidu" android:host= " Www.baidu.com "android:path="/news "/>  3). Categorygives additional information about the action to Execute. Note: The Android.intent.category.DEFAULT category must be specified in the project manifest's XML file Intent filter, activities will very often need to the Category_default So, they can be found by context.startactivity,or Context can ' t the acitivity Component[java]  i Ntent.addcategory ("Com.baidu.category.TEST");  [html] <!--must specify Category_default, only so startactivity (intent) can find- <category android: Name= "Com.baidu.category.TEST"/>  <Category android:name= "Android.intent.category.DEFAULT"/>   In addition to the main attributes above, there are additional properties that can be enhanced. 4). TypeSpecifies an explicit type (a MIME type) of the intent data. [Java]  intent.settype ("Image/jpeg");  [html] View plaincopy<data android:mimetype= "image/*"/>   Note: The data Uri and type in the Java file cannot be set using their respective functions at the same time. Because the Uri is cleared when you use the type, you can use the Setdataandtype method to set [Java] intent.setdataandtype (Uri.parse ("baidu://www.baidu.com/ News ")," image/jpeg ");  [html]  <data android:mimetype= "image/*" Android:scheme= "Baidu" android:host= "Www.baidu.com" Android: Path= "/news"/>   (3). The use of the difference between the two uses   explicit intent is generally used internally in the application, because the name of the component is already known within the application, and can be called directly. When an app activates activity in another app, it can only use implicit intent to create an intent based on the intent filter configured by the activity so that the values of each parameter in the intent match the filter so that activity in the other app can be activated. Therefore, implicit intent is used between the application and the application.

Ntent 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.



Androge Feature Article-intent

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.