Intent and intent Filter

Source: Internet
Author: User
Tags home screen

Android applications have three core components: Activity, service, and broadcast referers run through messages that are called intentions. Intent messaging is a facility for late run-time binding between components in the same or different applications. the intent itself is an intent object that stores the abstract description of the operation to be executed-for broadcasts, it indicates the operation that has occurred and is about to be reported. The intents sending mechanism varies with the three components.

  • Transmits an intent object to the context. startactivity (intent) or activity. startactivity forresult (INT) to run an activity (you can call the activity in the activity that is started in this way. setresult () sets the result parameter, which will be received in the activity that starts the current activity --- it can be received through onactivityresult (INT requestcode, int resultcode, intent data)
  • Pass an intent object to context. startservice (intent) to start a service or pass a new command to a running service. In addition, you can bind a service through context. bindservice (intent. (Establish a connection between the calling component and the target service)
  • Transmits an intent object to any broadcast methods (for example, context. sendbroadcast (), context. sendorderedbroadcast (), context. sendstickybroadcast () the intent will be passed to all registered broadcast referers.

In the above three cases, when the intent is passed out, the android system will find the appropriate activity, service, or multiple broadcast referers to respond to this intent ., These three situations do not overlap, they are independent of each other and do not interfere with each other. (After context. startactivity () is called, intent will only be received by the corresponding activity)

 

Intent object

An intent object is an information package. It contains the information required by the component to receive this intent (for example, required action and required information) and the information required by the Android system (to process the category of the intent component and how to start it)

In general, intent object mainly includes the following information:

Component name

The name of the component that processes intent. This field isComponentnameObject --- It is the complete qualified name of the target component (package name + class name) for example: "com. Android,. Test. testactivity ".

This field is optional. If this field is set, the intent object will be passed to the instance of the class corresponding to this component name. If no settings are set, Android uses other information in the intent object to locate a suitable target component. (Called intent resolution... This will be discussed later)

You can set the component name through setcomponent (), setclass (), or setclassname. You can use getcomponent () to read data.

Action)

A string that represents the action to be executed.--Or, for broadcase intents, it indicates the action being taken and reported. Many action constants are defined in the intent class. As follows:

Constent (Constant)

Target component(Target component)

Action (Action)

Action_call

Activity

Initialize a call

Action_edit

Activity

Display the data to be edited by the user

Action_main

Activity

This activity is used as the first activity of the task. No data input or data is returned.

Action_sync

Activity

Synchronize data on the server on the device

Action_battery_low

Broadcast receiver er

Insufficient power warning

Action_headset_plug

Broadcast receiver er

Plug the headset into the device or remove it from the device.

Action_screen_on

Broadcast receiver er

The screen is lit.

Action_timezone_changed

Broadcast receiver er

Time zone settings change

You can also define your own action strings to activate the component. The custom action should contain the package name as the prefix: for example"com.example.project.SHOW_COLOR".

Action largely determines the structure of the remaining part of intent. ---- In particular, the data and extras fields. Just as the method name of a method usually determines the parameters and return values of the method. For this reason, you should name the action as clearly as possible. You can set the action through setaction () and get it through getaction.

Data

The data attribute consists of the Data Uri and the data MIME type. The definition of action often determines how data is defined. For example, if an intent action isACTION_EDIT The corresponding data should contain the URI of the data to be edited. If an action is:ACTION_CALL , Data should betel:The URI of the phone number. Similarly, if the action isACTION_VIEW Data should be:http: URI. The received activity will download and display the corresponding data.

When an intent matches a component that has the ability to process this intent, it is important to know the data type (MIME type) in addition to the data Uri. For example, a component that displays images should not play audio files.

In many cases, data type can be inferred from the URI. Especially: uri =content:Uris data is usually located on the current device and controlled by a content provider. Even so, we can explicitly set a data type on the intent object. the setdata () method can only set Uri, settype () sets MIME type, setdataandtype () can be set for both, get Uri and data type can respectively call getdata () and GetType () method.

Category

A string that contains the type information of the component that processes the intent and serves as a supplementary description of the action.

An intent object can have any number of category items. Like action, several Category constants are defined in intent class .. As follows:

Constant

Meaning

Category_browsable

The target activity can display data in a browser.

Category_gadget

The activity can be embedded inside of another activity that hosts gadgets.

This activity can be included in another activity that loads gadgets.

Category_home

The activity displays the home screen, the first screen the user sees when the device is turned on or when the home key is pressed.

Category_launcher

The activity can be the initial activity of a task and is listed in the top-level application launcher.

Allows an activity to appear in launcher.

Category_preference

The target activity is a preference panel.

This activity is an option panel

Addcategory () Add a category

Removecategory () deletes a category ()

Getcategorys () Get all category ()

Extras

Additional information in the form of key-value pairs. for example, the intent of action_timezone_changed has a "time-zone" additional information to indicate the new time zone, while action_headset_plug has a "state" additional information to indicate whether the headset is inserted or pulled out.

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

Flags

There are a variety of logos, many of which indicate how the Android system starts an activity (for example, the activity should belong to that task) and how to treat it after it starts (for example, whether it belongs to the list of recent activities ). All these flags are defined in the intent class.

 

 

Intent resolution

Intent has two forms:

L explicitly specify a target component through its name (Component Name field), because the component name is usually not known to other application developers. Therefore, explicit communication is often used for messages within an application. ---- For example, an activity starts a subordinate service or another activity

L implicit intent: do not specify the target component name (component name is empty) Implicit intent is usually used to activate components of other applications

Android delivers an instance to the specified target class. The passed intent object only defines the component name -- it determines which component will be used to process the intent.

Different policies are required for implicit intent. In the absence of a specified target, the Android system must find the most suitable component to process this intent-a single activity or service to execute a request action or a group of broadcase referers to respond to broadcast notifications.

It compares the content in the intent object with the intent filter (intent filters. The Android system opens a component that can receive intent according to the intent filter. If a component does not have an intent filter, it can only accept the explicit intent. If yes, it can accept both ..

Only three aspects of an intent object are consulted when the object is tested against an intent filter:

When an intent and intent filter are compared, only the following three aspects are considered:

Action
Data (both Uri and data type)
Category

Intent Filters

To tell the Android system which intent they can process, activities, services, and broadcast receivers must set one or more intent filters. Each filter describes a component's ability to filter out unwanted intent and leave what you want. You do not need to consider this.

A filter contains three attributes of an intent object: Action, data, and catrgory. An implicit intent must pass these three tests to the component that contains the filter.

Test 1: Action Test

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

As shown in the instance, when an intent object can only name a single action, a filter can list multiple actions. This list can also be empty. A filter must contain a <action> element. Otherwise, it will prevent all intents from passing this test, the specified action in intent must match one of the actions listed in the filter. If no action is specified for an intent object or filter. The result is as follows:

L if a filter does not specify any action, no intent will be matched. Therefore, all intent will not pass this test.

L on the other hand, if no action is specified for an intent object, the test will be passed automatically-as long as there is at least one action in the filter

Test 2: Category Test

<Intent-filter...>

<Category Android: Name = "android. Intent. Category. Default"/>

<Category Android: Name = "android. Intent. Category. browsable"/>

...

</Intent-filter>

To pass the category test, each category contained in the intent object must match one of the filters. Filter can list additional category, but it cannot miss any category contained in the intent object.

In principle, an intent object without any categorys will always pass this test. In most cases, it is correct. However, there are also exceptions. Android considers all implicit views passed into startactivity () to contain at least one category --- "android. intent. category. default ".. therefore, activities that want to receive these implicit intentions must include "android. intent. category. default ".. yes (for "android. intent. action. main "and" android. intent. category. the filter of launcher is an exception. Because they indicate that this activity has enabled a new task and will appear in the auncher screen. They can also contain "com. Intent. Category. Default", but not necessary)

Test 3: Data Test

Similar to action and categories, data is also a sub-node in intent filter. You can set multiple data nodes or none.

For example:

<Intent-filter...>

<Data Android: mimetype = "Video/MPEG" Android: Scheme = "HTTP".../>

<Data Android: mimetype = "audio/MPEG" Android: Scheme = "HTTP".../>

...

</Intent-filter>

Each <DATA> element can specify a URI and a data type (MIME media type). Uri has the following attributes: schema, host, port, path

Schema: // host: Port/path

For example:

Content: // com. example. Project: 200/folder/subfolder/etc

In the preceding example, the schema is content: Host: COM. example. Project.

Port: 200 path: Folder/subfolder/etc

Host host and port form URI authority. If no host is specified, the port is ignored.

<DATA> attributes of nodes are optional, but they are not independent of each other. To make an authority meaningful, you must specify scheme. If path is meaningful, scheme and Authority (HOST: Port) must be specified.

When the URI in the intent object is compared with the intent filter, only the department is compared. For example, if a filter only specifies one scheme, all Uris containing this scheme will match. If a filter only specifies scheme and authority, and does not have a path, then all contents including this scheme and authority will match. If a filter specifies a scheme, authority, and a path, only the same scheme, authoritym, and path will match. However, for path, we can use wildcards for department matching.

<data>NodetypeThe Property specifies the MIME type of data. It is more common than the URI in the filter. Both the intent object and the filter can use the "*" wildcard as the child type-for example :"text/*"Or"audio/*"--- Indicates that all child types match.

Data test compares the URI and data type in the intent object with the one specified by the filter. The rules are as follows:

A) if a intent does not specify the URI and data type, if the filter is the same, the test is passed.

B) if an iintent has a URI but does not have a data type (or the data type cannot be inferred from the URI), it can only use the filter: URI match and does not specify a type. this case is limited to a URI similar to mailto: And Tel: That does not specify the actual data.

C) if an intent contains data type but does not have a URI, the same data type is listed in the filter and the URI is not specified.

D) If an intent contains a URI and data type (or the data type can be inferred from the URI), the filters list the same data type, the URI of the intent object either matches the URI in the filter, or the URI of the intent iscontent:Orfile: The filter does not specify the URI.

If an intent can be filtered by multiple activities or filters, you will be asked which component to activate. If none of them exist, an exception is thrown.

 

Common Cases

This rule is for Rule D in data test. It indicates that a component can obtain local data from a file or content provider. Therefore, filters can be used to set data type and it is not necessary to explicitly name schemecontent:Andfile:.

The following<data>Indicates that the android component can obtain image data from the content provider and display it.

<Data Android: mimetype = "image/*"/>

Because most of the available data is provided by the content provider, the filter that specifies the data type but does not specify the URI is the most common case.

Another common configuration is filters with a scheme and a data type. For example, A <DATA> element like the following tells android that the component can get video data from the network and display it:

Setting scheme and data type is another common configuration. The following<data>To tell android that this component can obtain and display video from the Internet.

<Data Android: Scheme = "HTTP" Android: TYPE = "Video/*"/>

Consider what the browser application does when a user clicks a link on a web page. It first tries to display the data (as an HTML page for processing ). If it cannot display this data, it uses an implicit intent to set scheme and data type to start an activity that can display this data. If the receiver is not found, it will call the Download Manager to download the data and place it under the control of the content provider, so that many activitys (those named datatype) can process the data.

Most applications can be started independently without referencing special data. For the activities to start the application, you must set "android. Intent. Action. Main" as action.

If you want to display it on the program launcher, you must set "android. Intent. Category. launcher" to category.

<Intent-filter...>

<Action Android: Name = "code Android. Intent. Action. Main"/>

<Category Android: Name = "code Android. Intent. Category. launcher"/>

</Intent-filter>

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.