About the seven properties of intent

Source: Internet
Author: User

Forgive me for being foolish, intent the concept of seven properties I was only yesterday to contact, looked at, are some commonly used things, is not summed up, then today to briefly summarize.

Intent seven properties refer to Intent's componentname, Action, Category, Data, Type, Extra, and flag, seven attributes, which can be divided into 3 categories:

First class: Start, with ComponentName (Explicit), Action (implicit), Category (implicit).

The second type: the value, with data (implicit), Type (implicit), Extra (implicit, explicit).

Class III: Boot mode with flag.

Now, let's say that.

1.ComponentName

Component itself has the component meaning, we can set up component to start other activity or other activity in the application, to see a simple example:

Start another activity in the same app:

Intent = new Intent (); Intent.setcomponent (New ComponentName (this, secondactivity.class)); StartActivity (intent);
This startup method is equivalent to the following two startup modes:

Intent = new Intent (This,secondactivity.class); startactivity (intent);

Intent = new Intent (); Intent.setclass (this, secondactivity.class); startactivity (intent);
Of course, we can also launch activity in other apps by setting the ComponentName property, and you can refer to the use of componentname for this piece of content. Let's take a look at implicit startup.

2.Actionand category

Because in actual development, the action is mostly used in conjunction with the category, so here we put these two together to explain. Action in Intent we use a lot of the radio, in the activity, we can set the action to implicitly start an activity, such as we have a thirdactivity, we do the following configuration in the manifest file:

        <activity            android:name= ". Thirdactivity "            android:label=" @string/title_activity_third ">            <intent-filter>                <category Android:name= "Android.intent.category.DEFAULT"/>                <action android:name= "com.qf.ThirdActivity"/>            </intent-filter>        </activity>
When we make this configuration in the manifest file, our thirdactivity will respond to this action, so how can we respond? See below:

Intent = new Intent (); Intent.setaction ("com.qf.ThirdActivity"); StartActivity (intent);
Of course, we can also write a little bit simpler, as follows:

Intent = new Intent ("com.qf.ThirdActivity"); StartActivity (intent);
In this way we can also start an activity, then you may also notice that our manifest file has a category node, then no this node can? No!! When we start an activity with this implicit startup, the action and category must match, and the activity will start successfully. If we do not define category, then we can use the system default category for the time being, in short, category cannot be not. This time we may have a question, if I have more than one activity configured with the same action, then which will start? Take a look at the familiar picture below:

When we have multiple activity configured with the same action, then the system will pop up a selection box, let us choose to start the activity.

Action we can only add one, but category can add multiple (at least one, no need to set to default), as follows:

        <activity            android:name= ". Thirdactivity "            android:label=" @string/title_activity_third ">            <intent-filter>                <category Android:name= "Android.intent.category.DEFAULT"/>                <category android:name= "mycategory"/>                < Action android:name= "com.qf.ThirdActivity"/>            </intent-filter>        </activity>
Corresponding to our start-up method can also be modified, as follows:

Intent = new Intent ("com.qf.ThirdActivity"); Intent.addcategory ("mycategory"); StartActivity (intent);

3.Data

By setting up data, we can perform calls, text messages, develop Web pages, and so on. What kind of operation do you want to look at in our data format:

Open Web Page Intent = new Intent (Intent.action_view); Intent.setdata (Uri.parse ("http://www.baidu.com")); StartActivity ( intent);//Call intent = new Intent (Intent.action_view); Intent.setdata (Uri.parse ("tel:18565554482")); StartActivity ( Intent);
When our data is an HTTP protocol, the system will automatically find the activity that can open the HTTP protocol, this time if the phone installed more than one browser, then the system will pop up a number of browsers for us to choose from. This is when we start an activity by setting up data, and we can also set a data property to publish our activity for others to call, how to publish it?

        <activity            android:name= ". Httpactivity "            android:label=" @string/title_activity_http ">            <intent-filter>                <action Android:name= "Android.intent.action.VIEW"/>                <category android:name= "Android.intent.category.DEFAULT"/ >                <data                    android:scheme= "http"/>            </intent-filter>        </activity>

In the data node we set the protocol that our activity can open, which we set as the HTTP protocol, and then when we open an HTTP request, the system will let us choose whether to open it with this activity. Of course, we can also define a protocol ourselves (our own defined protocol, because others do not know, so it can only be opened by our own programs). such as the following:

        <activity            android:name= ". Httpactivity "            android:label=" @string/title_activity_http ">            <intent-filter>                <action Android:name= "Android.intent.action.VIEW"/>                <category android:name= "Android.intent.category.DEFAULT"/ >                <data                    android:scheme= "myhttp"/>            </intent-filter>        </activity>
So how do we open our own activity?

Intent = new Intent (); Intent.setdata (Uri.parse ("myhttp://www.baidu.com")); StartActivity (intent);
This example doesn't make any sense, I just give a custom protocol to the chestnuts.

In fact, here, we should understand why we say that data is an implicit value, such as when we open a Web page, the HTTP protocol followed by the web address, we do not have to specify which page to open.

4.Type

Type exists primarily for the purpose of further description of the type of data, but in general, the Type property is valid only if the Data property is null, and if the data property is not NULL, the system automatically parses it according to the protocol in data. Instead of tube type, let's take a look at the following section of the source code:

    /** * Set The data This intent are operating on. This method automatically * clears any type that is previously set by {@link #setType} or * {@link #setTypeAndNor     Malize}. * * <p><em>note:scheme matching in the Android framework is * case-sensitive, unlike the formal RFC.     As a result, * you should always write your Uri with a lower case scheme, * or use {@link uri#normalizescheme} or * {@link #setDataAndNormalize} * To ensure, the scheme is converted to lower case.</em> * * @par     AM data The Uri of the data this intent are now targeting.     * * @return Returns the same Intent object, for chaining multiple calls * to a single statement.  * * @see #getData * @see #setDataAndNormalize * @see android.net.uri#normalizescheme () * */Public Intent        SetData (Uri data) {mdata = data;        Mtype = null;    return this;     }/** * Set an explicit MIME data type. * * &LT;p>this is used to create intents this only specify a type and not data, * For example to indicate the type of dat     A to return. * * <p>this method automatically clears any data, is * previously set (for example by {@link #setData})     . * * <p><em>note:mime type matching in the Android framework is * case-sensitive, unlike formal RFC MI  ME types. As a result, * you should always write your MIME types with lower case letters, * or use {@link #normalizeMimeType } or {@link #setTypeAndNormalize} * To ensure the it is converted to lower case.</em> * * @param type T     He MIME type of the data being handled by this intent.     * * @return Returns the same Intent object, for chaining multiple calls * to a single statement. * * @see #getType * @see #setTypeAndNormalize * @see #setDataAndType * @see #normalizeMimeType * * p        Ublic Intent setType (String type) {mdata = null;Mtype = type;    return this; }

When we set the data, the system will default to the type NULL, when we set the type, Data is set to NULL by default. That is, in general, the data and type we only need to set a line, if we want to set the data and want to set the type, then you can use
Setdataandtype (Uri data, String type)

This method to complete. Let's take a look at a music player by setting the type to intent. The code is as follows:

Intent = new Intent (); intent.setaction (Intent.action_view); Uri data = Uri.parse ("file:///storage/emulated/0/xiami/audios/passive. mp3"); Intent.setdataandtype (data, "Audio/mp3"); StartActivity (Intent);
If we are going to open a video file, the type will be set to "video/*", where * means all video files are supported.

5.Extra

Extra better understand, we often use it to pass data between activity, extra can pass the basic type, string type and implement the Serializable or Parcelable interface class, the specific usage does not say much.

6.Flag

By setting flag, we can set an activity's startup mode, which is basically the same as Launchmode, so I won't dwell on the use of Launchmode. See Launchmode

About the seven properties of 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.