An analysis on the matching principle of Android Intentfilter

Source: Internet
Author: User

1 Intent is divided into two main categories, explicit and implicit.

An explicit event is an event that explicitly specifies the target component through the component Name property.

For example, we create a new Intent, named after the event is used to start an Activity called "Com.silenceburn.XXXX", then this is an explicit event.

An implicit event is an event that does not explicitly specify a target component without the component Name property.

For example, the system sent to all the programs that monitor the phone call. "Event, because the system is not sure who will handle the event, so the system does not explicitly specify the target component, i.e. there is no target component, then this is an implicit event.

Here is just an introduction to explicit and implicit events, more precise and detailed description please refer to the SDK documentation, we just need to remember that the biggest difference between the two events is whether the component Name property is empty.

2 event filtering policies and Intentfilter

The system is convenient for transmitting explicit events, because if intent is compared to a letter, then component name is a detailed recipient address, and the system can accurately deliver explicit events to the target component.

When transmitting an implicit event, it is more troublesome. Because the envelope on this letter does not have a letter of delivery address!

What about that? The system made a difficult decision to take the letter apart. Find the right recipient through the clues in the content of the letter.

For example, the clue in the letter describes: "The addressee is male, fast 30 years old, unmarried, like playing games", then the system in the community to find such people.

It is very fortunate that the people of this community are very high quality, every family has written a little self-introduction at the door,

For example, Zhang San wrote: "I am a male, achild, unmarried, like to play games," John Doe wrote: "I am a woman, fast 30 years old, unmarried, like shopping" and so on.

With each family's self-introduction, the system will be able to quickly locate the real recipients!

The above is an example of an analogy, but the strategy for dealing with implicit events in the Android system is basically the above pattern.

First the system will observe the contents of the intent (open the letter to see the content), to obtain a matching clue, the system requires the following three kinds of clues:

Action

Data (both URI and data type)

Category

Second, each component in the system, if it wants to receive an implicit event, must declare its own intentfilter (self-introduction, what kind of letter I am interested in).

As for how to write Intentfilter, it is quite clear that it should be written like this:

"I am the component XXXX, I want to receive such an implicit event: its action must be XXX, its category must be YYYY, it contains the data must be ZZZZ"

If the component does not declare intentfilter, then all implicit events are not sent to the component. (Note that this does not affect sending an explicit event to the component.) )

For each implicit event that occurs in the system, the system attempts to match the action, data, category, and the Intentfilter declared by each component in the system to find the appropriate recipient.

3.IntentFilter Matching principle

For an explicit event, the system can be delivered precisely. For an implicit event, the system parses the event's action, data, category content, and matches each component's declared Intentfilter to find the matching component for delivery. Action and category there's nothing to say, so I'll describe the most complex data matching deployment:

It is important to first recognize thatdata is a relatively complex element.

The data is described and positioned by a URI, which consistsof three parts,

Scheme://host:port/path mode :// host : Port / Path

Also in the event, you can set the MIME type of data as the DataType property of the event . For the convenience of the description, please note that Intentfilter is abbreviated as filter below.

First, a matching principle is defined, that is , for URI matching, only the part declared in filter is compared.

Partial matching principle: As long as the partial match declared in the filter succeeds, the entire URI match is considered successful.

For example, content://com.silenceburn.sdcardtester:1000/mydata/private/

And the filter is defined as content://com.silenceburn.sdcardtester:1000/can be matched.

Note that the path section is not defined in filter , but it can still be successfully matched because the filter does not declare parts that are not compared.

In other words, any event that matches content://com.silenceburn.sdcardtester:1000/, regardless of what path is, can be matched successfully.

Next is the true data section, which is the URI matching rule as follows:

1. If the URI and datatype of data are empty, then the URI and type of the filter must also be empty to match the success

2. If the URI for data is not empty, but datatype is empty, the filter must define the URI and match the success, and the type is empty to match the success

3. If the URI of data is NULL, but datatype is not empty, the filter must be null, the type is defined and the match succeeds in order to match the success

4. If both the URI and data of data are NOT NULL, both the URI and the type of the filter must be defined and matched successfully in order for the match to succeed. For the URI section, there is a special handling, even though the filter does not have a URI defined,content and file Two URIs also exist as existing URIs.

From the above description, you can understand why when registering the SD card Plug and unplug receiver, not only need

New Intentfilter (intent.action_media_mounted);              Intentfilter.addaction (intent.action_media_scanner_started);              Intentfilter.addaction (intent.action_media_scanner_finished);              Intentfilter.addaction (intent.action_media_removed);              Intentfilter.addaction (intent.action_media_unmounted);              Intentfilter.addaction (intent.action_media_bad_removal);

And you need to add

   Intentfilter.adddatascheme ("file");

Registering an application for loading and unloading events requires not only

       New Intentfilter ();              Intentfilter.addaction (intent.action_package_added);              Intentfilter.addaction (intent.action_package_removed);              Intentfilter.addaction (intent.action_package_changed);

and need

       Intentfilter.adddatascheme ("package");

The reason is that the data match.

Finally, the article is after I in the Internet, an article extracted and processed, first thanks to the original author's careful design, the original address is as follows:http://blog.csdn.net/silenceburn/article/details/6083375

An analysis on the matching principle of Android Intentfilter

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.