Android and androidsdk

Source: Internet
Author: User

Android and androidsdk

I wrote an Intent blog, mainly about Implicit Intent. Portal: Android-Intent

Intent object structure

Component name, Action, Data, Category, Extras, Flags

Component name

The Component name is the Component name of the Intent object to be processed.

The component name object is encapsulated by the ComponentName class. The component name contains the package name and class name and is declared in the AndroidManifest. xml file.

The component name is set through setComponent (), setClass (), setClassName (), and obtained through getComponent.

It should be noted that Component name is an option. If it is set, the Intent object explicitly specifies the Component to be switched. If it is not set, the Intent object needs to be filtered and searched based on other information.

Action

Action refers to the Action to be completed by Intent and is a String constant.

The Intent class has many predefined constants for some common actions, and some are defined elsewhere in the Android API. You can also define the Action constant by yourself. You need to add the package name of your application as the prefix for the custom constant.

Action largely determines how other parts of Intent are constructed, especially the data and extras fields. (As if the function name determines the same parameter value and return value .) Therefore, Action names should be as specific as possible, and they should be closely integrated with other fields in Intent.

Use setAction () and getAction () to set and read Action attributes.

Data

The Data attribute is the URI and MIME Type of the execution action. Different actions have different Data specifications.

For example, when Action is ACTION_EDIT, the data field will be the document URI; when Action is ACTION_CALL, the data field will be tel: URI with the phone number to be called; if Action is ACTION_VIEW, the data domain is http: URI.

It is important to know the data type (MIME type) when it matches the intent and the component that can process the data included in the intent. For example, a component that displays images should not be called to play an audio.

In many cases, we can see from the URI that the data type, such as content: URIs, indicates that the data is on the device, but is controlled by the content provider.

The data type can also be explicitly specified. For example, the setData () method specifies that the data is URI, setType () indicates MIME type, and setDataAndType () indicates that it is both URI and MIME type. When reading, URI uses getData () and MIME type uses getType ().

Category

Category is a string that provides additional information about the types of components that can process this Intent object.

An Intent object can contain any number of category descriptions.

Methods corresponding to category include adding addCategory (), removing removeCategory (), and obtaining all category getCategories ().

Extras

The extra data passed to Intent is defined as Bundle, which is some key-value pairs. It seems that some actions correspond to specific data Uris, and some actions correspond to specific extras.

For example, the ACTION_TIMEZONE_CHANGED intent object has an extra of "time-zone" to confirm the new time zone;

ACTION_HEADSET_PLUG has a "state" extra indicating whether the headset is inserted or not. There is also a "name" extra about the headset type;

If you want to design a SHOW_COLOR action, extra should contain the color value.

The Intent object has a series of putXXX () functions used to put data types, and a series of getXXX () functions are also used to read data.

In fact, data can be used as a Bundle object, using the putExtras () and getExtras () methods.

Flags

Various types of Flag. Many of them are used to specify how the Android system starts the activity and how to treat it after the activity is started. All of these are defined in the Intent class.

Intent filter

To tell the system which implicit intent can be processed, activity, service, and broadcast receiver have one or more intent filters.

Each filter describes a capability of a component and a set of intent that the component is willing to receive. In fact, it filters out intent of a specific type and does not want intent, but it can only exclude intent of an implicit type, an explicit intent can be directly transmitted to its target object. No matter what it contains, the filter will not be queried.

However, an implicit intent can only be passed to this component when one of its filters is used.

A component has various independent filters for all the work it can do.

An intent filter is an object of the IntentFilter class. However, because the Android system must know its capabilities before starting a component, the intent filter is generally not created in Java code, but in the manifest file of the application (AndroidManifest. xml) as the <intent-filter> element.

One exception is the filter of the broadcast receiver. They are dynamically registered through the Context. registerReceiver () method and will be directly created as an IntentFilter object.

When an Intent object and an intent filter are tested, only three aspects of the Intent object will be referred:

 

  • Action
  • Data (URI and data type)
  • Category

 

When the component is determined to receive intent, extras and flags do not work.

A filter contains a domain parallel to action, data, and category in the Intent object. An implicit intent will test all these three fields.

To pass to a component containing a filter, the intent object must pass all three tests.

If one of the tests fails,AndroidThe system will not set this intentObject passed to this component, at least not based on the filterComponents.

However, since a component can have multiple intent filters, one intent object may not pass one of the filters, but it may pass another.

Code Action

In manifest, the <intent-filter> element lists actions as its <action> sub-elements:

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

One filter can list multiple actions,This list cannot be blank, that is, a filterIt should contain at least one action,Otherwise, it will block any intent..

To pass this test,The specified action in the intent object should match one of the listed actions.If no action is specified for the intent object or filter, the result is as follows:

1. If the filter does not list any actions and there is nothing for intent to match, all intent will fail in the test. No intent can pass this test.

2. On the other hand, if an intent object does not specify an action, it will automatically pass the test. (As long as the filter contains at least one action ).

Code Category

<Intent-filter> categories are also listed as sub-elements, as shown in figure

<intent-filter . . . >    <category android:name="android.intent.category.DEFAULT" />    <category android:name="android.intent.category.BROWSABLE" />    . . .</intent-filter>

If an intent object wants to use category, each category in the intent object must match a category in the filter.

Filters can List redundant categories, but do not ignore any category in intent.

In principle, an intent object without any category should always pass the test. However, with one exception, Android considers all the implicit intents passed in the startActivity () method as containing at least one category: "android. intent. category. DEFAULT" (the CATEGORY_DEFAULT constant ).

Therefore, the activity that receives the implicit intent object must include "android. intent. category. DEFAULT" in their intent filter ". (Including "android. intent. action. MAIN "and" android. intent. category. the filter of LAUNCHER is an exception. They can contain "android. intent. category. DEFAULT ", but they do not need to do so .)

Code Data

Similar to actions and categories, data is also used as sub-tags, and may not appear or appear multiple times. 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 ).

Each URI contains the following attributes: scheme, host, port, path (for example, scheme: // host: port/path)

When a URI in an intent object is compared with the URI in the filter, it only compares with the URI section actually mentioned in the filter. For example, if a filter only specifies scheme, all URIs with that scheme will match this filter.

The path in the Filter can contain wildcards (wildcards ).

<Data> the type attribute in the element specifies the MIME type of the data, which is more common in filter than URI.

Both Intent objects and filters can use wildcard * to represent child types, such as "text/*" or "audio/*"-indicating any subtype matches.

 

The URI and data type must be tested for data testing. The rules are as follows:

A. An intent object. If neither the URI nor the data type is included, the test can be performed only when neither the filter nor the data type is included.

B. if an intent object contains only one URI and does not contain the data type (and the data type cannot be obtained from the URI), only when the URI matches the URI in the filter, if the filter does not contain the data type, you can test it. (This will be the case only for URIs like mailto: and tel: that do not refer to actual data .)

C. If an intent object only contains data types and does not contain Uris, it can be tested only when the filter contains the same data type and does not contain any Uris.

D. if an intent object contains both the URI and data type (or the data type that can be deduced from the URI ), only when its data type matches one of the Data Types in the filter, it can pass the test of the Data Type part. If you want to pass the test of the URI part, one case is that its URI matches the filter, and the other case is that intent contains content: or file: URI, while filter does not contain URI. In other words, if a filter only specifies the data type, the component supports content: or file: data in a predefined manner.

If an intent can be started through multiple activity or service filters, the user will be asked which component to choose to start; if no target is found, an exception will occur.

I am the dividing line of tiantiao
What is android

Android means a robot. android phones use android phones. Google developed android phones and android phones do not know what the android phones mean.

What is android

Android means a robot. android phones use android phones. Google developed android phones and android phones do not know what the android phones mean.

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.