Intent's things

Source: Internet
Author: User

Forgive me for interpreting the heart of a young man who is always running. However, the intent to be introduced today is no more than 2B. In my opinion, there may be some elegant taste. Where is the elegant? Then we will analyze the basic friendship between intent and its good partner intentfilter.

Opening confession

Intent is a lively little boy in the android family, and is a social networking boy from an early age. In the code, the call associations between activity, service, and broadcastreceiver are all communicated by intent, for example, startactivity () of activity (), service startservice (), bindservice (), broadcastreceiver sendbroadcast (), sendorderbroadcast (), and so on, all of which must carry an intent (or intentfilter) parameter.

Of course, you must specify the information of the component to be started in intent, or explicitly hide the information. Obviously, it is explicit start: for example, intent, you give this thing to XX (name) activity aunt. Concealed is implicit launch: Of course, it is not vulgar to give an example: intent. You handed this stuff to the fat aunt who danced square dance with me at the vegetable market last night, among them, the square dance and fat will give a big mom the positioning of the red fruit. This is the two matching methods of intent. I think it is strange and abstract.

The next step is a good time to introduce intentfilter, which is the son of the daemon (Component interceptor ). When intent is implicitly started, intent will find a group of friends, intentfilter, and then ask them, whose mom went to the market last night to perform a square dance? Who of you is so fat? Then intentfilter thought about each mom. Oh, one of them patted his head and said, isn't that my mom! Then he leads intent to find an activity mom and hand it over to her.

In order to prevent everyone from failing to understand the above example, I would like to develop the social responsibility of 2B young people, and introduce intent and intentfilter in a more conventional language: intent is an intent object, the component can start another component by setting intent. There are two methods: explicit start and implicit start. Explicit start directly specifies the name of the component to be started, for implicit start, specify action, category, data, and so on to start qualified components. intnetfilter is an intent filter, which sets filtering rules such as action, category, and data, if action, category, and data in intent match the filter, the filter component is enabled.

Code Point

The idea of attacking the city lion is that the Code is not liked, so I still have to honestly code a few codes to support the competition. However, the explicit start and implicit start of intent code are just a few pages, so I am sorry for the masses if I post them here. So, I will record the key code. If there is a loss of memory in the future, I can come back to investigate the easy-to-Forget knowledge.

Explicitly start activity and service:

Intent intent1 = new intent (getapplicationcontext (), myactivity. Class); startactivity (intent1); // start the activity component startactivityforresult (intent1, 1); // start the activity that has returned
Intent intent2 = new intent (getapplicationcontext (), myservice. Class); startservice (intent2); // start servicebindservice (intent2, Conn, 1); // bind a service

Implicitly start activity and service:

Intent intent1 = new intent (); intent1.setaction ("com. net168.test. action. myactivity "); startactivity (intent1); // start the activity component intent intent2 = new intent (); intent1.setaction (" com. net168.test. action. myservice "); startservice (intent2); // start servicebindservice (intent2, Conn, 1); // bind a service

Send broadcast:

Intent intent = new intent (); intent. setaction ("com. net168.test. action. mybroadcast "); sendbroadcast (intent); // send a normal broadcast sendorderedbroadcast (intent, null); // send an ordered Broadcast

It's actually very simple. I am sorry to introduce it...

The family of intent is entangled in intentfilter

The intent background is complex, and it is more complicated than some non-streaming Hei society. In general, Intent includes component, action, categroy, Data, type, extra, and flag, which are seven attributes. We will explain these attributes later.

Component: It must accept a componentname object. To create a componentname, you must specify the package name and class name. This uniquely identifies a component class, in this way, the program can start the corresponding components through the given component class. Therefore, we can re-enable the explicit and implicit start classes from here, generally, explicit start is performed when componentname is explicitly specified, and implicit start is performed on the contrary. In addition, we can directly input the context and class names in the constructor when instantiating the intent, or directly set the componentname, such as intent (this, myactivity. class), this method is recommended, relatively simple.

Action: The action must complete an abstract action, but it is not determined by which component to complete it. You need to determine the component based on intentfilter. in intent, there can be only one action, in intentfilter, there can be multiple actions. This is the most basic property for implicitly starting a component.

Category: It adds additional category information to the action, because the action can start multiple components, but if you analyze the components to be started in more detail, you can use category to intercept them; in intentfilter and intent, there can be multiple category. By default, any intentfilter has an intent. category_default attribute.

Data, Type: These two are relatively close attributes.

The data attribute is usually used to provide the operation data to the action. Its value is a URI object. The URI object meets the format of scheme: // host: Port/path, such as content: // COM. android. contacts/contacts/1, where content is part of scheme, Com. android. contacts is the host part, port part is omitted, and contacts/1 is the path part.

The Type attribute is used to specify the MIME type corresponding to the specified URI of the data. It can be a custom type, as long as the string conforms to the ABC/XYZ lattice.

In use, we need to know that these two attributes will overwrite each other: If intent sets the data attribute first, then the type attribute will overwrite the data attribute, and vice versa. Therefore, to set the data and type attributes, call the setdataandtype () method of intent.

Extra: Extra is used for multi-volume data exchange. Its object is a bundle object.

Flag: The flag attribute is used to add some additional control flag for the intent. You can use addflag () to add a control flag. Generally, you can use it to control the startup effect and method of the activity.

Among the seven attributes, the matching rules of intent and intnetfilter are action, category, data, and type. The specific rules are as follows:

Action matching rules:

1. If the intent contains action_a and the intentfilter contains the action_a string, the intent can be successfully matched by intnetfilter no matter whether there are other action_ B and action_c strings.

2. If intent contains action_a, intentfilter cannot match intnet as long as action_a does not exist in intnetfilter.

3. If no action_x exists in intent, the filter can match the intent as long as any action_x exists in intnetfilter.

4. If intentfilter does not have any action_x, no matter whether there is any action_x in intent or there is no action_x, the two cannot match at all.

Matching rules of category:

This matching relationship is relatively simple. As long as all the category in the intent exists in the intentfilter, the intent can match the intnetfilter, regardless of whether other category exists in the intentfilter. For example, if the intent contains category_a, category_ B, And the intentfilter contains category_a, category_ B, and category_c, you can match them. If the intentfilter contains category_a, category_d, and category_c, because no category_ B exists in intent in intentf, the matching fails.

Matching rules of data and type:

Data is described and located by Uris. Uris are composed of three parts,Scheme: // host: Port/pathMode ://Host:Port/Path

In addition, you can set the MIME type of data in the event as the datatype attribute of the event.

First, we define a matching principle, that is, Uri matching, which only compares the parts declared in intentfilter.

Partial matching principle: as long as the part declared in intentfilter matches successfully, the whole URI matches successfully.

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

And intentfilter are defined as content: // com. silenceburn. sdcardtester: 1000.

Note that the path part is not defined in intentfilter, but it can still be matched successfully because the parts not declared by intentfilter are not compared.

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

The following is the real data section, that is, the URI matching rules are as follows:

1. If the URI and ype of data are empty, the URI and type of the filter must be empty to match successfully.

2. If the data URI is not empty, but the ype is empty, the filter must define the URI and match successfully, and the type is empty to match successfully.

3. If the URI of data is empty but the ype is not empty, the filter must be empty, define the type, and match successfully.

4. If the URI and data of data are not empty, the URI and type of the filter must be defined and matched successfully to make the match successful. For the URI part, there is a special processing, that is, even if the intentfilter does not define the URI, the content and file Uris also exist as the existing Uri.

Intent's things

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.