Piglet's Android Getting Started day 6

Source: Internet
Author: User

Piglet's Android Getting Started day 6

Android Application Core: Intent (Intent)

-----Reprint Please specify the Source: Coder-pig


Introduction to this section:


Through the previous study, we all know can call startactivity (intent) or startactivityforresult (intent)

To start a new activity, and their arguments are all instances of the intent type, what is this intent?

What's the use? There is another problem, I believe that careful friends have found that Androidmanifest.xml's activity tag has a

Intent-filter, what is this? All right, take this. Because, let's get a little bit of the core of Android apps.

Intent (intention)!



Learning Roadmap for this section:



Learning route Map Analysis:

① understand what is called intent, what is he doing?

② distinguishes between explicit intent and implicit intent, starting a different way of activity!

③ understand the seven properties of intent, flags do not understand can be put on a temporary

④ uses the process of displaying intent and implicit intent to activate a component, as well as the predefined actions and custom actions of the implicit intent!



Body:


Related concepts of intent:





the difference between explicit intent and implicit intent:




Seven properties of intent:componentname (component name):



Action (Action) and category (category):




data and type (MIME type)




Extras (extra)




flags (Mark)




use of Intent:


an explicit intent:

This is one of the most common ways we've used before, usually starting a second activity, we just need to fill in the

The fully qualified class name of the activity, in the call startactivity () or Startactivityforresult ()


code example: Click the button to return to the home screen

:



Core code:

Intent it = new Intent ();
It.setaction (Intent.action_main);
It.addcategory (Intent.category_home);
StartActivity (IT);



code example Two: Click the button to open Baidu:

As follows:




Core code:

Intent it = new Intent ();
It.setaction (Intent.action_view);
It.setdata (Uri.parse ("http://www.baidu.com"));
StartActivity (IT);








Implicit Intent:






implicit intent of pre-defined actions:

code example: When the button is clicked, all activity for view is filtered out and further selected by the user:

Run:


Core code Flow:

Create a second activity layout, with the corresponding activity, add code to the first Activity's button click event:

Intent it = new Intent ();
It.setaction (Intent.action_view);
StartActivity (IT);

Finally, add the following code in intent:

<activity android:name= ". Secondactivity "
Android:label= "Second Activity" >
<intent-filter>
<action android:name= "Android.intent.action.VIEW"/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

Then you can, the code is simple!




implicit intent of custom actions

code example: activating another activity with a custom action and category

Run:


It is important to note that although we have customized a category, we still have to add this default, or it will be an error:

In the Second Activity tab of Androidmanifest.xml, add:

<category android:name= "Android.intent.category.DEFAULT"/>





related code Download:

Back to Home interface: code download

Open Browser: Code download

implicit intent with pre-defined actions: Code download

implicit intent that contain custom actions: code Download




PS: For some of the system predefined action and category, we can follow the needs of their own Baidu, the following time will be collated, write a blog post!

Of course you can also check the official documents in the sdk-->docs-->reference-->android--->content--->intent.html!



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.