Android--Intent

Source: Internet
Author: User

Intent

Android's basic design philosophy is to encourage the reduction of coupling between components, so Android provides intent (intent),

Intent provides a common messaging system that allows you to pass Intent between your application and other applications to perform actions and generate events.

Use Intent to activate three core components of the Android app: activity, service, and broadcast receiver (Broadcastreceiver).

An explicit intent

Calling the Intent.setcomponent () or intent.setclassname () or Intent.setclass () method explicitly specifies the Intent of the component name as an explicit intent, explicitly intending to explicitly specify which component the Intent should be passed to.

New Intent (); Intent.setcomponent (new componentname (mainactivity.  this, otheractivity. class));

Or

Intent.setclass (mainactivity.  this, otheractivity. class);

Or

New Intent (mainactivity.  this, otheractivity. class)

Implicit intent

Intent with no explicitly specified component name is an implicit intent.

The Android system finds the most appropriate component to handle this intent based on the action (action), category, data (Data:uri, and data type) set in the implicit intent, such as:

Uri uri = Uri.parse ("Tel:" +new Intent (Intent.action_call, URI);

For implicit intent, by specifying a intent-filter,intent filter (intent filter) is actually used to match the implicit Intent, when an intent object is matched by an intent filter, only three aspects are referred to: action, Data (URIs and data types) and categories.

ACTION

An Intent object can only specify an action name, and a filter may enumerate multiple action names. If the intent object or filter does not specify any action, the result is as follows:

    • If the filter does not specify any action, all intents are blocked, so all intents are tested to fail. There is no intention to pass this filter.
< Intent-filter > </ Intent-filter >
    • On the other hand, as long as the filter contains at least one action, an intent object without a specified action automatically passes this test

Category

For an intent that can be tested by category matching, the category in the intent object must match the category in the filter. This filter can enumerate another category, but it cannot be omitted from any of the categories in this intent.

In principle an object with no category should always be able to pass the match test, regardless of what is in the filter. In most cases this is the right one.
With one exception, Android treats all implicit intentions passed to startactivity () as if they contain at least one category: "Android.intent.category.DEFAULT" (Category_default constant).

So when customizing the Activity, don't assume that the light defines an action enough. This will always be an error when activating the Activity with StartActivity ().

The reason is that he internally asked to match the Android.intent.category.DEFAULT category.

Therefore, activities that want to receive implicit intent must include "Android.intent.category.DEFAULT" in their intent filters.
(Exceptions are filters with "Android.intent.action.MAIN" and "Android.intent.category.LAUNCHER" settings)

Data

When a URI in an intent object is used to compare to a URI in a filter, the various components of the URI are compared.
For example, if the filter only specifies a scheme, all of the scheme's URIs can match the filter;

If the filter specifies a scheme, hostname but no path, all URIs with the same scheme and hostname can match the filter, regardless of their path;

If the filter specifies a scheme, hostname, and path, only URIs with the same scheme, hostname, and path can match the filter.

Of course, a path specification in a filter can contain wildcard characters, so that only a partial match is required.

The data test compares both the intent object and the specified URI and data type in the filter. The rules are as follows:

    • An intent object that contains neither a URI nor a data type can pass the test only if the filter also does not specify any URIs and data types.
    • An intent object that contains a URI but no data type can pass the test only if its URI matches a URI in a filter that also does not specify a data type.
      This usually happens on a URIs like mailto: and Tel: such: they do not refer to actual data.
    • An intent object that contains a data type but does not contain a URI can pass the test only if the filter lists the same data type and does not specify a URI.
    • An intent object that contains both a URI and a data type (or a data type that can be inferred from a URI) can be tested if its type matches the type enumerated in the filter.

If its URI matches a URI in the filter or if it has a content: or file File:uri and the filter does not specify a URI, it can pass the test as well.

In other words, a component is assumed to support content: and file: Data if its filter lists only one data type.

I'm the dividing line of the king of the Land Tiger.

Write a demo about the implicit intent

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Intent learning. zip

Reprint Please specify source: Http://www.cnblogs.com/yydcdut

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.