Android Summary series: Android Intent

Source: Internet
Author: User

The importance of intent in Android is self-evident. This paper mainly summarizes some problems needing attention in the process of intent use.

1. Implicit intent Androidmanifest.xml declaration <intent-filter> related

As an "intent" intent, the Androidmanifest.xml declaration does not have an independent, so-called <intent> label form, but is attached to other application components (activity/broadcastreceiver/ Service) exists. On explicit intent and implicit intent categories, explicit intent directly corresponds to the component name, and implicit intent exists for child nodes in the component declaration <intent-filter>.

There are several points to note when declaring a <intent-filter>:

1) .<intent-filter> meaning "intent match", mainly through this "intent match" target component, are "intent" target object, for the activation activity of the implicit intent, If more than one target activity is available, a list popup will pop up for the user to select a specific startup object. In the <intent-filter> declaration, the main include <action>,<category> and <data> sub-tags.

2) .<action> as the "intention" of the specific action, in <intent-filter> is necessary to have, a <intent-filter> declaration can have multiple <action> sub-tags, A specific name that can match several different corresponding intent action,action is best preceded by the package name to ensure the uniqueness of the action;

3) .<category> as the "intent" category, the action must be in <intent-filter> for the implicit activity component. By default, the system automatically adds the category "Android.intent.category.DEFAULT" when you start activity in an implicitly intent manner. As a result, <intent-filter> must contain at least <category android:name= "Android.intent.category.DEFAULT"/> declarations. When and only if the following conditions are excluded:

1 <action android:name= "Android.intent.action.MAIN" />2 <category android:name= "Android.intent.category.LAUNCHER"/>

Here is a specific warning about category.default in the Android documentation:

Note:in order to receive implicit intents, you must include the Category_default CATEGORY in the intent filter. The methods StartActivity () and Startactivityforresult () treat all intents as if they declared the Category_default Catego Ry. If You don't declare this category in your intent filter, no implicit intents would resolve to your activity.
Android automatically applies the the Category_default CATEGORY to all implicit intents passed to StartActivity () and star Tactivityforresult ().  So if you want your activity to receive implicit intents, it must include a category for "Android.intent.category.DEFAULT" In it intent filters (as shown in the previous <intent-filter> example.

Similarly, a <intent-filter> declaration can have multiple <category> sub-tags.

4). A Activity/broadcastreceiver/service component declaration can contain more than one <intent-filter> tag, matching for a single <intent-filter> in turn , the general principle is that within a single <intent-filter> tag, each sub-label should be able to contain the intent in the code, which can be redundant but not less.

2. Use intent to launch additional application components in the code (ACTIVITY/BROADCASTRECEIVER/SERVICE)

1). For explicit intent, specify the class name of the target component directly, and the system will intent to the target component within the current app;

2). An explicit intent is used in a intent, and an implicit intent is used, which is directly based on the explicit intent;

3). Implicit intent must pass through such as Setaction (..) The intent method specifies the action, which is visible, and the action is unique in the implicit code, through Addcategory (..) METHOD specifies the category, since the category can have more than one;

4). Service launches inside the app are best used with explicit intent, as described in the Android documentation:

To ensure your app are secure, always use an explicit intent when starting a Service and does not declare intent filters for Your services. Using A implicit intent to start a service was a security hazard because you cannot being certain what service would respond T o The intent, and the user cannot see which service starts. Beginning with Android 5.0 (API level), the system throws a exception if you call Bindservice () with an implicit inten T.

This is visible: Android 5.0 starts, cross app Bindservice will no longer work.

5). In view of the fact that the broadcast receiver itself is decoupled, only implicit intent can be used when sending broadcasts;

6). When the specified activity component is not found, it throws a Activitynotfoundexception exception and crashes directly, at which time it can be resolveactivity (..) The way to judge first:

1 if NULL ) {2    startactivity (intent); 3 }

7). Determine whether two intent "intentions" are the same, use the filterequals (intent) method, note that only the description information is included in the intent match, and not as additional information such as extra and flag.

Filterequals (Intent Other) determine if and intents is the same for the purposes of Intent resolution (filtering). that's, if their action, data, type, class, and categories are the same. This does no compare any extra data included in the intents.
Filterhashcode () Generate hash code that matches semantics of filterequals ().

Android Summary series: Android Intent

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.