Android Learning Note-intent (i)

Source: Internet
Author: User

The intent object is described in the official Android API: It is a passive data structure holding an abstract description of a operation to be performed.< c0> It is a data structure that abstracts the operations that will be performed at a time, with the effect of connecting two different components during the program's run.

The introduction of the

Intent mechanism is a message mechanism for implementing communication between components of an Android application, android coupling between components of an application .

Intent can be understood as the communication medium or intent of different components. Intent messages can activate the Android program's three core components: Activity,service and broadcastreceiver .

The usage of intent is as follows:

I. Application of intent in activity:

Components of the 1.Intent:

  The intent object consists of the following six parts: Component name, Action, Data, Category, Extras, Flags.

Example of 2.Intent:

Pass a Intent object to context.startactivity (intent) or activity.startactivity forresult (int) To run an activity (you can call Activity.setresult () to set the result parameters in the activity that starts with this mode, This parameter will be received in the activity that initiates the current activity---can be received via Onactivityresult (int requestcode, int resultcode, Intent data).

A. Examples of no return parameters:

  

Android.content.Intent  requestintent =  new android.content.Intent (orginalactivity.  Class, destinationactivity. class );  StartActivity (requestintent);

B. There are examples of return parameters:

  Public classOrginalactivityextendsActivity { Public voidonCreate (Bundle saveinstancestate) {Super. OnCreate (saveinstancestate); FinalAndroid.content.Intent requestintent =NewAndroid.content.Intent (orginalactivity.class, Destinationactivity.class); Findviewbyid (R.id.button). Setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {startactivityforresult (requestintent, Requestcode);  }         }     ); }  protected voidOnactivityresult (intRequestcode,intresultcode,intent Intent) {     if(Requestcode = = Requestcode && ResultCode = =ResultCode) {Bundle ret=Intent.getextras (); If (ret!=NULL) {TextView TextView=(TextView) Findviewbyid (R.ID.TEXTVIEW01);         Textview.settext (ret.getstring ("key")); }     }     Else {         //execution code for other conditions    }  }  }  //end of class definition PublicclassDestinationactivityextendsActivity { Public voidonCreate (Bundle saveinstancestate) {Super. OnCreate (saveinstancestate); Findviewbyid (R.id.button1). Setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {android.content.Intent resultintent=Newandroid.content.Intent ();                 Resultintent.putextras ("Key", "values");                 Setresult (resultcode,resultintent);  Finish ();}}); }  }

3.intent-action

Specifies an intent action, usually the action is divided into a system-defined action and a custom action.

The action constant defined inside the a.intent is the system action as follows:

  

B. The custom action can be as follows:

  

<ActivityAndroid:name=". Orginalactivity ">         <Intent-filter>              <ActionAndroid:name= "SelectAction"/>              <categoryAndroid:name= "Android.intent.category.DEFAULT" />         </Intent-filter>     </Activity>          

C. Additions: the Meaning of Android.intent.action.MAIN

Android.intent.action.MAIN Flag Start Application When the activity, if there are more than Android.intent.action.MAIN, first start mainfest inside the first appeared android.inten T.action.main. If more than one activity registered in the Mainfest file has <intent filters> tag, specify action android:name= "Android.intent.action.MAIN", The system starts by default on the activity list from top to bottom in the first activtiy.

4.Intent Resolution (parsing intent)

Within our application, we need to send a intent message when one activity jumps to another activity, and we indicate in action the package name + class name of the activity being jumped. We can achieve a jump to another activity. This time the jump between the activity, we marshal the intent is explicit intent (explicit intent). Therefore, the display is generally used within the application.

We also have a situation where we don't know the exact name of the activity component we're going to jump from, i.e. we don't have a definite activity to handle our actions, we just know what we need to deal with the activity of our actions, The Android system must find the most suitable component to handle this intent. This time, our intent intent to specify the filter criteria will go to filter all registered in our Android system to meet the conditions of activity, then, usually let us choose which activity we need to use to deal with our actions. The Intent we marshal in this case is the implicit intent (implicit Intent). Here we elicit intent Filters to explain our intent filtering process.

5.Intent Filters

The Intentfilter object is responsible for filtering out the intent that the component cannot respond to and handling, and only receives the intent that it cares about. Intentfilter implements the "whitelist" management, which lists only the intent that the component is willing to accept, but intentfilter only filters the implicit intent, and the explicit intent is transferred directly to the target component. Android components can have one or more intentfilter, each of which is independent of each other and only requires one of the validations to pass through. In addition to the Intentfilter used to filter the broadcast can be created in code, other intentfilter must be declared in the Androidmanifest.xml file. There are fields in Intentfilter that correspond to intent for filtering action,data and category.

A. A intent object can only have one action, but an activity's intentfilter may have more than one action. The marshaled intent message satisfies the condition as long as one of the action is satisfied. If the Intent message does not contain a specific action behavior definition, the filter matches the Intent message as long as a description of the action behavior is defined in the filter rule;

B. The Intent message does not contain any URI data or information about the content of the specific data, only matches filters that do not contain any data filtering information, and the data type is inferred from the data in the Intent. The data type that is inferred in the Intent message matches the filter if it is included in the list of data types declared in the filter rule, otherwise the message will be filtered by the filter.

If a intent object contains a data type, but does not contain a URI: only if Intentfilter does not specify a URL, but only contains the data type and is identical to intent, it passes detection. If a intent object contains both a URI and a data type (or the data type can be inferred from a URI), the intent object can pass the check only if its data type matches the data type in Intentfilter and the URL check is passed.

Where the URL consists of four parts: it has four properties scheme, host, port, path corresponding to each part of the URI.

Example: Content://com.wjr.example1:121/files

Scheme section: Content

Host section: Com.wjr.example1

Port section: 121

Path section: Files

The host and Port sections together form the credentials for the URI (authority), and if the host is not specified, the port is ignored.

These four properties are optional, but they are not completely independent. To make authority meaningful, scheme must be specified. To make path interesting, scheme and authority must be specified. Path in Intentfilter can use wildcard characters to match the path field, and both intent and intentfilter can use wildcards to specify MIME types.

C.intentfilter can be set in more than one category,intent can also contain multiple category, only intent all the category can match to the category in Intentfilter, Intent to pass the check. That is, if the category collection in intent is a subset of the collection of category in Intentfilter, intent can pass the check. If the category is not set in intent, it can be checked by all Intentfilter category. If a intent is able to pass through the intentfilter of more than one component, the user may be asked to activate the component. If no target is found, an exception is generated.

  To summarize, the components of the application can declare one or more intentfilter in order to tell Android what implicit intent requests it can respond to and handle. Each intentfilter describes the ability of the component to respond to intent requests-what type of request behavior the component wants to receive, and what kind of request data.

  (This article as a personal study notes, there are not the wrong place, also ask you to correct me, about the Android intent learning, still in the finishing)

Android Learning Note-intent (i)

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.