Android Notes (74) Detailed intent

Source: Internet
Author: User

We most often use intent to achieve the activity between the jump, recently done an app to search for images from the system function, the use of intent SetType method and setaction method, online search, found that the implementation of the jump is only intent function of the bucket, now to the intent function to do a summary to make a memo.

We often use the following method to achieve a jump:

New Inent (mainactivity.  this, secondactivity. class ); startactivity (intent);

In fact, this writing can also be written

New Intent (); Intent.setclass (mainactivity. this, secondactivity. class ); startactivity (intent);

This start-up is called an "explicit intent" because it directly specifies the intent target

Another way to call it "implicit intent" is not to specify the target component directly, but to start the target component by setting the individual properties of the intent

Intent

Intent is composed of componentname , Action , Data , Category , Extra , Flag six. Composition, intent sets its corresponding properties by calling the setxxx () method, which details the individual properties

"ComponentName"

The componentname is used to identify the application component of the displacement, which indicates the desired intent, the name of which is the package name of the target component, combined with the desired class name, and the component name is an option during the intent pass. When it is specified, it is an explicit intent message, and when it is not specified, the Android system selects the corresponding component based on other information, as well as the Intentfilter filter criteria.

"Action"

The action is actually a string that describes the name of the action intent, and in the intent class, many string constants have been defined to represent different actions, and of course the developer can customize the action

Common action Constants

action_call--broadcast data in the enclosed phone number

action_edit--Open the Edit program for the specified data

action_view--open an application that can display data encapsulated in

action_main--declares the program entry, the activity does not receive any data, and when it ends, it returns any data

Action_boot_completed--braodcastreceiver action constant, indicating that the system is up and ready

Action_time_changed--braodcastreceiver action constant, which indicates that the system time is changed by setting

More can view http://www.cnblogs.com/xs104/p/5111858.html

Because of updates to the API, some names change, but the basic idea is the same.

"Data"

data is mainly the encapsulation of intent messages, which mainly describes the URI and type of the data that the intent action is manipulating. Different types of action will have different data packages, such as the phone's intent will encapsulate the tel://format of the telephone URI, and Action_view's intent data will encapsulate HTTP//format URI, The correct data encapsulation is also important for intent matching requests

"Category"

category is a description of the Target component class information, as well as a string object, a intent can contain more than one category, and category-related methods have three:

1. Add a Category--addcategory

2. Delete a category--removecategory

3. Get a Category--getcategory

The Android system also defines a set of constants to represent different categories of intent, and some common category constants

Category_gadget, which indicates that the target activity can be embedded in other activity.

Category_home, indicating that the target activity is the HOME activity.

Category_tab, indicates that the target activity is an activity under a tag of tabactivity.

Category_launcher indicates that the target activity is the first activity in the application to be executed.

Category_prefernce, indicates that the target activity is a preference-setting activity.

"Extra"

Some additional additional information is encapsulated in the extra , which exists in the form of key-value pairs, intent can be stored and obtained extra through the Putextras () and Getextras () methods, The Android system also defines some extra constants

EXTRA_CC, email cc person email address.

Extra_email, an array of strings with mail-sending addresses.

Extra_subject, describes the topic to send the message when you use the Action_send action.

Extra_text, describes the information to send text when using the Action_send action.

"Flags"

There are more than 20 flags of intent in Android, such as

Flag_grant_read_uri_permissionflag_grant_write_uri_permissionflag_debug_log_resolutionflag_from_backgroundflag _activity_brought_to_frontflag_activity_clear_taskflag_activity_clear_topflag_activity_clear_when_task_ Resetflag_activity_exclude_from_recentsflag_activity_forward_resultflag_activity_launched_from_historyflag_ Activity_multiple_taskflag_activity_new_taskflag_activity_no_animationflag_activity_no_historyflag_activity_no _user_actionflag_activity_previous_is_topflag_activity_reset_task_if_neededflag_activity_reorder_to_frontflag_ Activity_single_topflag_activity_task_on_homeflag_receiver_registered_only

But we only use 4 of them.

Flag_activity_clear_top

For example, the current stack situation is: A B C D. D jumps through intent to B at this point, if the intent adds flag_activity_clear_top tag, the stack becomes: A B. If you do not add this tag, the stack will become: A b C D B. That is, if the flag_activity_clear_top tag is added and the target activity already exists in the stack, the activity on top of that target activity will be ejected from the stack for destruction. This is similar to setting the launch mode of B to Singletask.

Flag_activity_no_history
For example, the current stack is: A B C. C jump to D through intent, this intent add flag_activity_no_history flag, then the interface displays the contents of D, if at this point in D and jump to E, the case of the stack becomes: A B C E, at this time press the return key will go back to C, Because D is not pushed into the stack at all.

Flag_activity_single_top
Similar to the Launch mode singletop above activity. If a intent adds this flag, and the target activity of this intent is the activity at the top of the stack, then a new instance will not be pressed into the stack.

  

Flag_activity_new_task
For example, the case of stack 1 now is: A B C. C jumps to D through intent, and this intent adds a flag_activity_new_task tag, and if D this ACTIVITY adds a TASK affinity to the declaration in Manifest.xml, And unlike the affinity of stack 1, the system will first look for a task stack with the same task affinity as D, if it exists, press D into that stack, and if it does not, it will press it into the stack of a new D affinity. If the TASK affinity of D is not set by default, or is the same as the affinity of stack 1, it will be pressed into the stack 1 and become: a B C D, so it is the same as the non-flag_activity_new_task tag effect.

  Note If you attempt to start an activity from a non-activity, such as starting an activity from a service, intent must add flag_activity_new_task tags.

MORE: Flag in the intent

Intenfilter

Intentfilter describes what kind of intent objects a component is willing to receive, and Android abstracts it into a Android.content.IntentFilter class. In the Android androidmanifest.xml configuration file, you can specify its intentfilter for an activity via the <intent-filter> node, To tell the system what type of Intent the activity can respond to. Such intent are called implicit intent.

When a programmer uses startactivity (intent) to start another activity, if you specify intent the component attribute of the object directly, the activity The manager will attempt to start the activity specified by its Component property. Otherwise, Android will look for the most matching boot from all the activity installed in the system via the other properties of intent. If no suitable activity is found, the application gets an exception thrown by the system.

Intent's match

The intent parsing mechanism is primarily to find a matching intent by locating all intentfilter in the androidmanifest.xml and the intent defined therein. in this parsing process, Android is judged by the three attributes of the action, type, and category of intent, judging by the following:

1 If the action is specified by intent, the action must be included in the Intentfilter action list of the target component, otherwise it will not match;

2 If the intent does not provide a type, the system will get the data type from it. As with action, the data type list of the target component must contain the intent data type, otherwise it cannot be matched.

3 If the data in intent is not a content: type URI, and intent does not explicitly specify its type, the match will be based on scheme (such as http: or mailto:) of the data in intent.

As above, the Intent scheme must be present in the scheme list of the target component.

4 If intent specifies one or more categories, they must all appear in the list of organized categories.

For example, the intent contains two categories: Launcher_category and Alternative_category, and the parsed target component must contain at least these two categories. Each implicit intent that is emitted through the startactivity () method has at least one category, which is "Android.intent.category.DEFAULT",

So as long as the activity that wants to receive an implicit intent should include "Android.intent.category.DEFAULT" category, otherwise it will cause intent match to fail.

Android Notes (74) Detailed intent

Related Article

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.