Intent Android Detailed

Source: Internet
Author: User

Intents and Intent Filters three types of application basic components
Activity, service, and broadcast receiver--are activated using a message called intent.

Intent messaging is a mechanism for running-time binding between components. Intent is a intent object that contains a description of what needs to be done, or, for a broadcast, contains the message content being notified. There are different mechanisms for sending intent to these three types of components:
Use Context.startactivity () or Activity.startactivityforresult () to pass in a intent to start an Activity.
Using Activity.setresult (), pass in a intent to return the result from the Activity.
Pass the intent object to Context.startservice () to start a service or send a message to a running service.
Pass the intent object to Context.bindservice () to bind a service.
The intent object is passed to Context.sendbroadcast (), Context.sendorderedbroadcast (), or context.sendstickybroadcast (), which is passed to the broadcast method. Broadcast receiver.

In each of the three cases above,

The Android system will find the right activity, service, or broadcast receivers to respond to intent. The intent of the three are independent of each other.

Intent Objects Intent Object A Intent object contains information about the component that accepts the Intent (such as the desired action and the data needed for the action) and the information needed by the Android system (such as the category of the component, and how to start it).

Specifically, the component name is a ComponentName object. It is a combination of the full name of the target component (for example, "com.example.project.app.FreneticActivity") and the package name (such as "Com.example.project") set by the application manifest file.

The package name part of the former is not necessarily the same as the latter. The component name is optional. If set, the intent object is passed to an instance of the specified class.

If not set, Android uses other information to locate the appropriate target. The component name is set using SetComponent (), SetClass (), or setclassname (), and is obtained using getcomponent ().

Action A string that is named for the requested action, or, for broadcast intent, the action that occurs and is being reported.

Example: Constant target component action

Action_call activity initiates a phone call.

Action_edit activity displays the data to the user for editing.

Action_main activity starts the activity as the first activity of a task, does not pass in the parameter, and does not expect a return value.

Action_sync activity synchronizes the data on the device with a server.

Action_battery_low broadcast receiver emits a low battery warning.

Action_headset_plug broadcast receiver A headset is being inserted or unplugged.

The ACTION_SCREEN_ON Broadcast receiver screen is lit.

Action_timezone_changed broadcast receiver time zone settings change.

You can also define your own action string to start your application. The custom action should contain the package name of the application. For example, "Com.example.project.SHOW_COLOR".

The action largely determines the structure of the other part of the intent, just as a method name determines the parameters it accepts and the return value.

Therefore, it is best to give the action a name that best reflects its role. The action in a intent object is read and written using Getaction () and Setaction ().

The data needs to manipulate the URI and its MIME (multi-purpose Internet Mail extension, Multipurpose Internet Mail Extensions) type.

For example, if the action is Action_edit, then data will contain the URI to be edited. If action is action_call, data will be the URI for Tel: phone number.

If the action is Action_view, then data is http: The URI of the network address. When a intent is matched to a component, it is also important in addition to the out-of-URI data type.

For example, a program that displays pictures should not be used to process sound files. Data types can often be inferred from URIs, especially Content:uri, which indicates that the data belongs to a content provider.

However, the data type can also be declared by the intent object. The SetData () method sets the URI, and the Settype () method specifies the MIME type, Setdataandtype () sets the data URI and MIME type.

They can be read using GetData () and GetType (). Category A string that contains information about the kind of component that handles the intent. A intent object can have any category.

The intent class defines a number of category constants,

For example: constant meaning category_browsable target activity can use a browser to display-a sample or e-mail message.

Category_gadget the activity can be included in another activity that loads the gadget.

Category_home the activity shows the main screen, which is the interface that the user presses the home button to see.

Category_launcher the activity can act as the first activity for a task and is listed in the Application Launcher.

Category_preference the activity is an options panel.

The Addcategory () method adds a category to a intent object, Removecategory deletes a category, and GetCategories () gets all the category intent. Extras is a key-value pair in the form of additional information.

For example, Action_timezone_changed's intent has a "time-zone" additional information to indicate the new time zone, and Action_headset_plug has a "state" additional information to indicate whether the headset is plugged in or unplugged.

Intent object has a series of put ... () and set ... () method to set and obtain additional information. These methods are similar to bundle objects. In fact additional information can be read and written using Putextras () and Getextras () as bundles. Flags of various flags.

Many flags indicate how the Android system initiates an activity (for example, which task the activity belongs to) and how to handle it after startup (for example, whether it belongs to the recent activity list). Android systems and applications use intent objects to send systems to broadcast and activate system-defined components.

Intent Resolution Intent Analysis

There are two types of intent: Explicit intent use the name to specify the target component. Because component names are generally not familiar to other developers, this intent is typically used for internal messaging applications-such as an activity that initiates an affiliate service or another activity. Implicit intent does not specify the name of the target. Components that are typically used to start other applications. Android sends an explicit intent to the specified class. The name of the intent object is the only object that decides to accept intent.

For an implicit intent, the Android system must find the most appropriate component to handle it. It compares the contents of intent and intent filter. Intent filter is a related structure of a component that represents its ability to accept intent.

The Android system opens the components that can accept intent based on intent filter. If a component does not have a intent filter, it can only accept explicit intent. If so, they can be accepted at the same time. When a intent and intent filter are compared,

Only three attributes are considered: action, data, category. Extra and flag are not used in intent parsing. Intent filters activity, service and broadcast receiver can have multiple Intent filter to tell the system what implicit Intent they can accept.

Intent filter's name is Image: It filters out the intent you don't want to accept, leaving the intent you want to accept.

An explicit intent ignores intent filter. A component has a separate filter for everything that can be done. For example, the Notepad program's noteeditor activity has two filter--

One launches and displays a specific record to the user to view or edit, and the other initiates an empty record for the user to edit. Filters and security filter and secure a intent filter is not necessarily safe and reliable.

An application can allow one of its components to accept implicit intent, but it does not prevent the component from accepting the display intent. Other programs can always invoke the component using the custom data plus an explicit program name.

A intent filter is an instance of the Intentfilter class, but it generally does not appear in the code, but instead appears in the form of an Android manifest file. (one exception is that the intent filter for broadcast receiver is dynamically set using Context.registerreceiver (), and its intent filter is also created in the code.)

A filter has action, data, category and other fields.

An implicit intent must pass 3 tests in order to be accepted by a intent filter.

For a intent to be accepted by a component, it must pass one of its intent filter. Action test ...

A Intent object can specify only one action, and a intent filter may specify more than one action. The action list cannot be empty, otherwise it will organize all the intent.

An action for a intent object must match one of the actions in the intent filter to pass. If the action list of the intent filter is empty, it does not pass. If the intent object does not specify an action, and the action list of the intent filter is not empty, it is passed. Category test ...

Note that the constants for the action and category are used in the code, not in the manifest file, as mentioned earlier. For example, the Category_browsable constant corresponds to the expression "Android.intent.category.BROWSABLE" in the XML.

A intent to pass the category test, then each category in the intent object must match one of the filter. Theoretically speaking,

A intent object should be able to pass any category test if it does not have a category specified.

One exception: Android considers all implicit intent passed to startactivity () as having at least one category: "Android.intent.category.DEFAULT".

Therefore, an activity that wants to accept an implicit intent must include "Android.intent.category.DEFAULT" in the intent filter. (Exceptions to the intent filter for "Android.intent.action.MAIN" and "Android.intent.category.LAUNCHER").

They do not require "Android.intent.category.DEFAULT".) Data test ...

Each element specifies a URI and a data type. Each part of the URI is a different attribute-scheme, host, Port, Path:scheme://host:port/path

For example, in the following URI: Content://com.example.project:200/folder/subfolder/etc scheme is "content", host is "Com.example.project", Port is "$" and path is "folder/subfolder/etc".

Host and port together make up the URI authority. If host is not specified, port is ignored. These properties are optional, but they are not independent of each other: to make a authority meaningful, you must specify a scheme.

To make a path meaningful, you must specify a scheme and a authority. When the URI in the intent object is compared to the intent filter, it is compared to only the parts defined in filter.

For example, if scheme is defined in filter, all intent objects that contain the URI of the scheme pass the test. For path, you can use wildcard characters to make partial matches. The element's Type property specifies the data type.

It is more common in filter than in URIs. Both the intent object and the filter can use the "*" wildcard as a subtype.

For example, "text/*" "audio/*" means that all subtypes are matched.

The rules for the data test are as follows: A intent object that does not contain a URI or data type is passed only by a filter that is not contained by either.

A intent object with a URI but no data type (and cannot infer the data type from the URI) can only be matched by such filter:uri and does not specify a type. This situation is limited to similar mailto: and Tel: This does not specify the actual data URI.

A intent that contains only the data type but does not contain a URI is only through the filter: The filter lists only the same data type, and does not specify a URI.

A intent object that contains both a URI and a data type is matched only by the data type of the Filter:intent object and a type in filter, and the URI of the intent object matches the URI of the filter. Either the URI of the intent object is content: or file:, and filter does not specify a URI.

If a intent can pass more than one activity or service filter, then the user may be asked which one to start.

If none of them is true, then an exception is thrown. Common cases common situation the last rule above (d) illustrates that a component can typically fetch data from files and content provider.

Therefore, their filter can only list data types without scheme. This is a special case.

The following elements tell Android that the component can obtain image data from a content provider and display it: Because most of the available data is provided by content provider, the filter that specifies the data type but does not specify a URI is the most common case.

Another common configuration is that filter has a scheme and a data type. For example, the following elements tell Android that the component can get image data from the network and display it: Consider the browser's actions when a user clicks on a webpage.

It first attempts to display this data (as an HTML page to process). If it cannot be displayed, an implicit intent is created and an activity that can handle it is started.

If there is no such activity, then it requests the download Manager to download the data. Then it puts the data under the control of a content provider,

This allows a lot of activity (with a filter with only data types) to handle this data. Most applications also have a way to launch them independently, without referencing any particular data.

The activity that initiates the application has a filter with the action "Android.intent.action.MAIN". If they need to be displayed in the Application Launcher,

They must specify the category of "Android.intent.category.LAUNCHER". The using intent matching uses intent matching intent and intent filter to match,

Not only to find and launch a target component, but also to find information about the components on the device. For example, the Android system launches the Application Launcher, which is located on the top level of the screen, showing the programs that the user can start,

This is done by finding all the action on the device as "Android.intent.action.MAIN", category "Android.intent.category.LAUNCHER" intent The activity in which the filter is implemented.

It then displays the icon and title of the activity. Similarly, it locates the main screen program by looking for the filter "Android.intent.category.HOME".

Applications can use intent matching in the same way. Packagemanager has a set of query ... () method to find all the components that accept a particular intent,

and a series of resolve ... () method to determine the best component to respond to a intent. For example, Queryintentactivities () returns a list of activity

These activity can perform the incoming intent. Similarly, there are queryintentservices () and Queryintentbroadcastreceivers (). Note Pad Example

Example: Notepad Notepad sample program allows users to browse a list of notes, view, edit, delete and add notes. This section focuses on the intent filter defined by the program.

In its manifest file, the Notepad program defines three activity, each with at least one intent filter. It also defines a content provider to manage the note data.

The manifest file is as follows: The first activity, notelist, differs from other activity because it operates a directory of Notes (note list) instead of a separate note. It is generally used as the initial interface of the program.

It can do the following three things:

The filter declares the main entry for the Notepad application. The standard main action is a program entry that does not require any additional information (such as data, etc.), and the LAUNCHER category indicates that the portal should be listed in the Application Launcher.

This filter declares what the activity can do to a directory of notes. It allows users to view or edit the directory (using view and edit action), or select specific notes (using pick action). The mimetype of the element specifies the type of data that the action can manipulate.

It indicates that the activity can get the cursor (VND.ANDROID.CURSOR.DIR) of one or more data items from a content provider (Vnd.google.note) that holds Notepad data.

Note that the filter provides a default category. This is because the context.startactivity () and Activity.startactivityforresult () methods treat all intent as containing the default category,

There are only two exceptions: an explicit indication of the intent of the target activity name. Contains the intent of the main action and launcher category.

Therefore, the DEFAULT category is required in addition to the filter for main and launcher. This filter describes the ability of the activity to return a user-selected note without needing to know the directory.

The Get_content action is similar to the pick action. In both, the activity returns the URI of the user-selected note. (returned to call Startactivityforresult () to initiate activity for notelist activity.)

Here, the caller specifies the data type selected by the user instead of the directory of the data. This data type, Vnd.android.cursor.item/vnd.google.note, represents the type of data that the activity can return--

A URI for a note. From the returned URI, the caller can from the content provider holding the note data (vnd.google.note)

Get a cursor for a project (Vnd.android.cursor.item). That is, for pick, the data type indicates that activity can give the user an explicit data type. For Get_content filter,

It represents the data type that activity can return to the caller. The following intent can be accepted by notelist activity: Action:android.intent.action.MAIN

Do not specify any data to start the activity directly. Action:android.intent.action.MAIN Category:android.intent.category.LAUNCHER

Do not specify any data to start the activity directly. This is the intent that the program launcher uses.

Activity for all filter using this combination

is added to the initiator. Action:android.intent.action.VIEW data:content://com.google.provider.notepad/notes

Requires activity to display a list of notes, which is located in Content://com.google.provider.notepad/notes.

The user can browse the list and get information about the list item. Action:android.intent.action.PICK data:content://com.google.provider.notepad/notes

Request activity to display a list of notes under Content://com.google.provider.notepad/notes.

The user can pick a note and the activity returns the URI of the note to the activity that initiated the notelist. Action:android.intent.action.GET_CONTENT data Type:vnd.android.cursor.item/vnd.google.note requests activity to provide an entry for the Notepad.

The second activity, Noteeditor, displays a note to the user and allows them to edit it.

It can do the following two things: the main purpose of this activity is to enable users to edit a note--view or edit a note. (in category, Edit_note is a synonym for EDIT.)

Intent contains a URI that matches the MIME type Vnd.android.cursor.item/vnd.google.note uri--that is, a particular note.

It is generally returned by the pick or get_content action in the notelist activity. As before, the filter lists the default category. The second purpose of the activity is to enable the user to create a new note and insert it into the existing Notes directory.

The intent contains a URI that matches the vnd.android.cursor.dir/vnd.google.note, which means that the pen has these capabilities,

Noteeditor

You can accept the following Intent:action:android.intent.action.VIEW data:content://com.google.provider.notepad/notes/id

Requires activity to display notes for the given ID. Action:android.intent.action.EDIT data:content://com.google.provider.notepad/notes/id

Ask the activity to display a note with the specified ID, and then let the user edit it. If the user saves the changes, the activity updates the content provider data.

Action:android.intent.action.INSERT data:content://com.google.provider.notepad/notes

Ask activity to create a new empty note in Content://com.google.provider.notepad/notes, and allow the user to edit it,

If the user saves the change, the URI is returned to the caller. The last activity, Titleeditor, allows the user to edit the caption of the note.

This can be done by invoking the activity directly (setting the component name in intent). But here's a chance to show you how to do something else on the existing data (similar to the open in Windows, List of programs--translator note):

It must be called on a particular note (data type vnd.android.cursor.item/vnd.google.note), just like the previous view and edit action. However, the activity here shows the caption contained in the note data,

Rather than content. In addition to supporting the default category, the title editor supports an additional two category:

Alternative and selected_alternative. These category marks indicate that activity can be presented to the user in the Options menu (just like the launcher category indicates that activity can be the same in the program launcher).

Note Filter also provides a display label (android:label= "@string/resolve_title") to better control what the user sees in the Options menu.

With these abilities, the following intent can be accepted by Titleeditor: Action:com.android.notepad.action.EDIT_TITLE data:content:// COM.GOOGLE.PROVIDER.NOTEPAD/NOTES/ID requires activity to display the title of the given note ID and allows the user to edit the caption.

Intent Android Detailed

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.