What is intent?

Source: Internet
Author: User
Tags home screen

First, understand intent:

In an Android application, it is composed of four components, four of which can refer to "the composition of Android apps". And these four kinds of components are independent, they can call each other, coordinate work, and finally form a real Android application.

In the communication between these components, mainly by the Intent assistance to complete.  Intent is responsible for the action of an operation in the application, the action involves data, the additional data is described , Android according to this Intent description, is responsible to find the corresponding component, the Intent passed to the calling component, and complete the call of the component. As a result, intent plays a role as a media intermediary, specifically providing information about the components that are called to each other, and decoupling the caller from the callee.

  For example, in a contact maintenance app, when we click on a contact on a Contacts list screen (assuming the activity is listactivity), you want to be able to jump out of the contact's details screen ( Assuming that the corresponding activity is detailactivity) in order to achieve this goal, Listactivity needs to construct a Intent, which is used to tell the system that we want to do a "view" action, which corresponds to the View object is "a contact", Then call StartActivity (Intent Intent), the constructed Intent incoming, the system will be based on the description in this Intent, to manifest to find the activity that satisfies this Intent requirements, the system will call the found Activity, that is, detailactivity, the final incoming intent,detailactivity will be based on the description in this intent to perform the corresponding operation.

Intent are divided into two main categories: dominant (Explicit), recessive (implicit).

The dominant intent->> when jumping between two activity if using intent's setclass to set the initiator and receiver of the intent, or to construct the initiator and receiver when the object is directly new ;

The recessive intent->> does not need to use SetClass or setcomponent to specify the event handler, and the configuration in Androidmenifest.xml can be used by the platform to locate the consumer of the event .
Example code:
1. Explicit:

1 //Way One:2Intent Intent =NewIntent (Test. This, Testb.class);3 startactivity (intent);4 //Way Two:5Intent Intent =NewIntent ();6Intent.setclass (Testbundle. This, Target.class);7Intent.putextras (Mbundle);//Passing Information8StartActivity (Intent);

2. Implicit

// Browse the Web (System pre-defined configuration) Uri uri = uri.parse ("http://www.google.com"new  Intent (Intent.action_view,uri); StartActivity (intent);

Ii. composition of the intent

to pass data between different activity , it is necessary to include something in the intent, generally the most basic of the data should include :
"Action"

Used to indicate what action is to be implemented, such as Action_view, action_dial, etc.;

Specifically, you can refer to the Android.content.intent class in Android sdk-> reference, where all the actions are defined in the constants .

"Data"

The specific data to be factual is generally represented by a Uri variable;

Here are some simple examples:

Action_view CONTENT://CONTACTS/1//Display identifier contact information for 1
action_dial CONTENT://CONTACTS/1//Call this contact.

In addition to the two most basic elements of action and data, intent includes some other elements:
"Category"

This option specifies some additional information about the action that will be executed;

For example:

  Launcher_category that the recipient of intent should appear as a top-level application in LAUNCHER;

  alternative_category indicates that the current intent is one of a series of optional actions that can be executed on the same piece of data;

You can also refer to the Android.content.intent class in Android sdk-> reference.

"Type (data type)"

Explicitly specify the data type of the intent (MIME);

The data type of the general intent can be judged based on the data itself, but by setting this property, you can force an explicitly specified type to no longer be inferred.

"Component (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.

"Extras (Additional information)"

is a collection of all other additional information;

You can use extras to provide extended information for your component, such as, if you want to perform the "Send e-mail" action, you can save the e-mail message's title, body, and so on in extras, to the e-mail sending component.

Here are a few examples of these extra attributes:
Used to Launch home screen.

Action_main with category Category_home

The phone number used to list everyone in the list

Action_get_content with MIME type Vnd.android.cursor.item/phone
As you can see, action, Data/type, category, and extras together form a language that can be used by Android to express phrases such as "Call Zhang San".

Three, the analysis of intent

Components of an application to tell Android that it can respond to and handle implicit Intent requests, you can declare one or more Intent Filter. Each intent filter 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.

For example , the intent filter of a Web browser program should declare that the intent ACTION it wants to receive is web_search_action, and that the request data associated with it is the Web address URI format.

  How do I declare my own intent Filter for a component?

->>>> Common approach is to describe the Intent Filter of a component in a Androidmanifest.xml file with properties < intent-filter>.

As we mentioned earlier, the three elements of implicit intent (Explicit Intents) and Intent Filter (implicit Intents) are intent actions , data , and categories . In fact, an implicit intent request can be passed to the target component, which is required to pass these three checks. If any aspect does not match, Android will not pass the implicit intent to the target component. Next we explain the specific rules of these three aspects of inspection.
  1. Action Test
Java code

/*     < intent-filter> elements can include child elements < action>    such as:*/<intent-filter ...> <  Action android:name= "Com.example.project. show_current"/> <action android:name=" Com.example.project. show_recent"/> <action android:name=" Com.example.project. show_pending"/></intent-filter>

A < intent-filter> element should contain at least one < Action>, otherwise no intent request can match the < intent-filter>.

If the intent requested action and < intent-filter> a certain < action> match, the intent passes the action test for this < intent-filter>.

If the specific action type is not specified in the intent request or < intent-filter>, the following two scenarios occur.
(1) If no action type is included in < intent-filter>, then no intent request can match this < intent-filter>;

(2) Conversely, if the action type is not set in the intent request, then as long as < intent-filter> contains the action type,

This intent request will pass < Intent-filter> 's behavior test smoothly.

  2. Category Testing
Java code

/*     < intent-filter> elements can contain < category> child elements    such as: */    <intent-filter ...>    <category Android:name= "Android. Intent.category. DEFAULT"/>    <category Android:name=" Android. Intent.category. browsable"/>    </intent-filter>

The intent request passes the test only if all of the category in the intent request exactly matches the < category> of one of the intentfilter in the component, intentfilter the excess < category The > declaration does not result in a match failure. A intent-filter that does not specify any category tests will only match Intent requests that do not have a category set.
3. Data testing
Java code

<intent-filter. >    <data android:type
Android:scheme= "http"
Android:host= "www.google.com" .../></intent-filter>

The intent element specifies the data URI and data type of the request that you want to accept, and the URI is divided into three parts: scheme, authority, and path. The URI data type of the Inteat request set with SetData () must be the same as specified by scheme in Intentfilter. If authority or path are also specified in the Intent-filter, they also need to be matched before passing the test.
  4. Simple example Description
After explaining the basic concepts of intent, we then use intent to activate the phone dialer that comes with Android, and through this example you will find that using intent is not as difficult as the concept described.

The code that eventually creates the intent is as follows:
Intent Intent = new Intent (Intent. action_dial,uri.parse ("Tel:13800138000″)";
Once you've created your intent, you can tell Android that you want to start a new activity.
StartActivity (Intent);

Four, the intent of the structure function

public constructors:
1, Intent () empty Constructors
2, Intent (Intent o) copy Constructor
3, Intent (String action) specifies the constructor of the action type
4 , intent (String action, Uri URI) specifies the action type and the constructor of the URI, URIs are primarily data sharing between programs ContentProvider
5, Intent (Context packagecontext, Class &NBSP;CLS) The constructor for the incoming component, which is the
6, Intent (String action, Uri Uri, Context Packagecontext, mentioned above), CLASS&NBSP;CLS) The first two combinations
intent have six constructors, 3, 4, 5 are the most commonly used, not other useless! The action of the
Intent (String action, Uri URI) is the Name property value corresponding to the action node in Androidmainfest.xml. A number of action and category constants are defined in the intent class.
Sample code:

Intent Intent = new Intent(Intent. Action_edit, null);

startactivity (intent);

Example code two uses a fourth constructor, except that the URI parameter is null. When executing this code, the system will look in the program master configuration file Androidmainfest.xml
The corresponding activity, if it corresponds to more than one activity, will pop up a Dailog select activity, such as:
This is not the case if you are sending in sample code one way.

V. Use of intent to transfer data between activity

Main code in main:

1 New  Bundle(); 2 bundles. Putstringarray ("Namearr", Namearr); 3 New Intent (Main.  this, countlist. class  ); 4 Intent. Putextras (bundle); 5 startactivity (intent);

To execute code in Countlist:

 This . getintent (). Getextras  = bundle. Getstringarray ("Namearr");

The above code implements the data transfer between activity ~

What is intent?

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.