Android Development Tutorial: Understanding Intent and intent Filter

Source: Internet
Author: User

In the Android Help documentation, intent is introduced as an intent are an abstract description of a operation to be performed, meaning a intent is the abstraction of an upcoming operation, in Tent's literal sense is "intent", and the three other application basic components of Android applications--activity, service, and broadcast Receiver are "activated" using messages called intent. There are different mechanisms for sending intent to these three types of components:

Use Context.startactivity () or Activity.startactivityforresult () to pass in a intent to start an Activity. Using Activity.setresult (), pass in a intent to return the result from the Activity.
Pass the intent object to Context.startservice () to start a service or send a message to a running service. Pass the intent object to Context.bindservice () to bind a service.
The intent object is passed to Context.sendbroadcast (), Context.sendorderedbroadcast (), or context.sendstickybroadcast (), which is passed to the broadcast method. Broadcast Receiver.
In the above three cases, the Android system will find the appropriate activity, Service, or broadcast receivers to respond to intent. The intent of the three are independent of each other. One might ask, for example, launching another activity in Android can be used directly with context.startactivity (Class startclass) The way to start an activity, why do you need to use intent, not a little "off the pants fart"-superfluous? In fact, although this method is simple, to deviate from the original Android design, Android uses the intent encapsulation program "invoke intent", whether it is to start an activity or a service or to start a broadcastreceiver, Can use a unified intent this "start intent", so that the use of a consistent programming model, but also a high level of "decoupling."

1, Intent Introduction

Intent represents the startup "intent" of Android apps, and the Android app will launch the specified component according to intent, as to which component to start, depending on the properties of intent. Here's a look at the various developments in intent and how Android launches the corresponding components based on different properties. Intent is made up of component, Action, Data, Category, Extra, and flag, which will be described in detail in the next chapter.

(1) Component name

The component name is actually a ComponentName object that identifies the unique application component, which indicates the desired intent component, which is composed of the class name of the target component combined with the package name of the target component. During the intent pass, the component name is an option, and when it is specified, it is an explicit intent message, which we call "explicit," and when it is not specified, the Android system chooses the corresponding component based on other information and Intentfilter filter conditions. , we call it "implicit intent".

Create a ComponentName objectComponentNameComponentName= New ComponentName(intentdemoactivity., secondactivity.intent Intent = new intent (); //settings intent Component Properties intent.componentname//start secondactivitystartactivity (intent                

The code above can actually be simplified to:

Intent=newIntent(intentdemoactivity.  This,secondactivity.  Class); //Start secondactivitystartactivity(intent);         
(2) Action

The action is actually a string that describes the name of the action that intent triggered, and in the intent class, a lot of string constants have been defined to represent different actions, and of course developers can customize the action, which is also very simple to define. The action name must be a unique string, so a good habit is to use a naming system based on a Java package naming method.

There are a number of system-defined action constants, and some of the more common ones are listed below.

Action_call, dial out the phone number encapsulated in data.
Action_edit, open the encoding program for the data specified in
Action_view to open an application that can display the data that is encapsulated in it.
Action_main, which declares the entry of a program, does not receive any data and does not return any data at the end. The
Action_boot_completed,broadcastreceiver ACTION constant indicating that the system has started. The constant of the
Action_time_changed,broadcastreceiver ACTION, which indicates that the system time is changed by setting.

                Declares a Intent object  Intent=newIntent();  /** * Set the ACTION property, here is the jump to the dial-up interface * action_dial = "Android.intent.action.DIAL"; */Intent.  Setaction(Intent.  Action_dial); startactivity(intent);             

The action largely determines the structure of the other part of the intent, just as a method name determines the parameters it accepts and the return value. Therefore, it is best to give the action a name that best reflects its role. An action in a intent object is read and written using Getaction () and Setaction ().

(3) Data

Data is mainly the encapsulation of intent messages, mainly describing the URI and type of the data to which the action of intent is manipulated. Different types of actions will have different data packages, such as the phone's intent will encapsulate the tel://format of the telephone URI, and Action_view's intent data will encapsulate the http: format URI. The correct data encapsulation is also important for intent matching requests.

              Declares a Intent objectIntentIntent= New Intent (); /** * Set the ACTION property, here is the skip to dial-up interface * action_dial = "Android.intent.action.DIAL"; */intent. (intent. Action_dialuri Uri =uri.. Setdatauriintent       
(4) Category

Category is a description of the Target component class information. Also as a string object, a intent can contain multiple category. There are three methods related to category, addcategory add a category,removecategory to delete a category, and GetCategories get a category. The Android system also defines a set of static character constants to represent different categories of intent, and some common category constants are listed below.

Category_gadget, which indicates that the target activity can be embedded in other activity.
Category_home, indicating that the target activity is the HOME activity.
Category_tab, indicates that the target activity is an activity under a tag of tabactivity.
Category_launcher indicates that the target activity is the first activity in the application to be executed.
Category_prefernce, indicates that the target activity is a preference-setting activity.

A intent can contain a maximum of one action property, but one intent may contain multiple category properties.

(5) Extra

Some additional additional information is encapsulated in extra, which is in the form of a key-value pair. Intent can store and get extra through the Putextras () and Getextras () methods. In the intent class of the Android system, some common extra key values are also defined, as shown below.

EXTRA_BCC, an array of strings with mail-sent addresses.
Extra_email, an array of strings with mail-sending addresses.
Extra_uid, when using the Action_uid_removed action, describes the ID of the deleted user.
Extra_text, describes the information to send text when using the Action_send action.

(6) Flag

Some of the flags about how the system starts the components are also encapsulated by Android.

Introduction to 2.IntentFilter

Intentfilter is actually the equivalent of a intent filter, and once an application is developed, it needs to tell the Android system what stealth intent requests it can handle, which requires declaring intentfilter. The use of Intentfilter is actually very simple, declaring only what kind of intent requests the application receives.

Intentfilter filter intent, generally through the action, data and category three aspects of monitoring. Next, the three aspects are introduced respectively.

(1) Check action

A intent can only set one action, but a intentfilter can set multiple action filters. When Intentfilter sets more than one action, the action validation is done with a single satisfaction. When no action is stated in Intentfilter, any action will not match it. If the intent does not contain any action, the match succeeds as long as the intentfilter contains an action.

(2) Check data

Data monitoring mainly consists of two parts, namely the data URI and data type, and the data URI is divided into three parts to match (scheme, authority, PATH), only these all match, data validation will succeed.

(3) Check category

Intentfilter can also set multiple category, and when the category in intent matches a category in Intentfilter, it is checked by category, and the other category is not affected. However, when Intentfilter does not have a category set, it can only match intent that do not have the category set.

http://blog.csdn.net/wuwenxiang91322/article/details/7671593

Android Development Tutorial: Understanding Intent and intent Filter

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.