Android Learning Notes (iv)

Source: Internet
Author: User

An application is composed of many activities that use intent to move between activities.

The intent is divided into two explicit and implicit types:

1. Display Intent:

Create a new layout file named Second_layout.xml and the code is as follows

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <ButtonAndroid:id= "@+id/button_2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/app_button_2"    /></LinearLayout>

Then create a new secondactivity that inherits from the activity, with the following code:

 package   Com.gui.activitytest;  import   android.app.Activity;  import   Android.os.Bundle;  import   Android.view.Window;  public  class  secondactivity extends   Activity { protected  void   OnCreate (Bundle Savedinstancestate) { super  .oncreate (        Savedinstancestate);        Requestwindowfeature (Window.feature_no_title);    Setcontentview (r.layout.second_layout); }}

Finally register the secondactivity in Androidmanifest.xml, the registration code is as follows:

< Activity      Android:name =". Secondactivity "></activity>

Because Secondactivity is not a master activity, you do not need to configure the contents of the <intent-filter> tag.

Starting a second activity from the first activity requires the introduction of a intent.

Intent is an important way to interact between components in an Android program, which can indicate the current
* The action that the component wants to perform, or the parameters can be passed between different components.
* Purpose: Intent is used to launch events, start services, and send broadcasts.
* Usage: Intent broadly divided into two types: explicit intent and implicit intent
* Example: Intent has multiple overloads of constructors, one of which is
* Intent (Context packagecontext,class<?>cls).
* This constructor receives two parameters: the first parameter context requires a startup activity
* Context, the second parameter class is the specified target activity that you want to start.
* Use: an startactivity () method is provided in the activity class, which is used to start the activity
* It receives a intent parameter. You can start the target activity by passing the built-in intent into the StartActivity () method.

How do I start a second activity? To modify the Click event of a button in Firstactivity, the code is as follows:

        // An explicit call to intent    New Intent (firstactivity.  this, secondactivity. class );    StartActivity (intent);

The meaning of the above code is to first construct a intent, pass in Firstactivity.this as the context, and pass in the Secondactivity.class as the target activity. Then through the StartActivity () method

To execute the defined intent.

2. Implicit intent

Implicit intent does not explicitly indicate which activity to start, but instead specifies a series of abstract action and category information, which is then referred to the system to determine which activity to start.

By configuring <intent-filter> on the <activity> tab, you can specify the action and category that the current activity responds to, such as opening androidmanifest.xml and adding the following code:

<ActivityAndroid:name=". Secondactivity ">        <Intent-filter>           <ActionAndroid:name= "Com.example.activitytest.ACTION_START"/>           <categoryAndroid:name= "Android.intent.category.DEFAULT" />        </Intent-filter>        </Activity>

* indicated in the <action> tab that the current activity can respond Com.example.activitytest.ACTION_START
* This action. The <category> tag contains additional information that accurately indicates that the current activity can
* The category may also be in the intent of the response.
* Only the content in <action> and <category> can match the action and category specified in intent to respond to intent.

After configuring the contents of the <intent-filter> tag, then modify the Click event of the button in the Firstactivity code as follows:

New Intent ("Com.example.activitytest.ACTION_START"); startactivity (Intent);

Using another constructor of intent, the action string is directly passed in, indicating that you want to start an activity that responds to the action of Com.example.activitytest.ACTION_START.
Android.intent.category.DEFAULT is a default category that automatically adds the category to intent when the StartActivity () method is called.

Only one action can be specified in each intent, but it is possible to specify multiple category.

For example, to modify the button's Click event in Firstactivity, the code is as follows:

// implicit use of intent New Intent ("Com.example.activitytest.ACTION_START"); Intent.addcategory ("com.example.activitytest.MY_ CATEGORY "); StartActivity (intent) ;

Code, you can use the Addcategory () method in intent to add a category, where a category is customized with a value of Com.example.activitytest.MY_CATEGORY.

After you add a category to your code, you must add the category that is declared in the code in the <intent-filter> tag of the secondactivity in the Androidmanifestxml file.

There are many uses of implicit intent to initiate activities, and the activities of other programs can be initiated using implicit activities.

For example, to modify the Click event of a button in Firstactivity, the code looks like this:

    // using implicit intent, you can not only start your own internal activities, but also start activities    of other programs New Intent (intent.action_view);    Intent.setdata (Uri.parse ("http://www.baidu.com"));    StartActivity (intent);

The above code first specifies that intent's action is Intent.action_view, which is an Android built-in action with a constant value of Android.intent.action.VIEW.

The URL string is then parsed into a URI object through the Uri.parse () method, and the URI object is passed in by calling intent's SetData () method. Re-run the program will get

Case:

The SetData () method receives a URI object that is used primarily to specify the data that is being manipulated by the current intent, which is usually generated in the form of a string that is parsed in the Uri.parse () method.

You can also configure a <data> tag in the <intent-filter> tab to specify more precisely what type of data the current activity responds to.

The main contents of the <data> tag can be configured:

(1) Android:scheme is used to specify the protocol portion of the data, such as the HTTP section above.

(2) Android:host is used to specify the hostname portion of the data, such as the www.baidu.com section above.

(3) Android:port is used to specify the port portion of the data, which is typically followed by the host name.

(4) Android:path is used to specify the host name and the part after the port, such as the content after a certain URL followed by the domain name.

(5) Android:mimetype is used to specify the type of data that can be processed and is allowed to be specified using wildcards.

Only the content specified in the <data> tag is identical to the data that is carried in the intent, the current activity should be intent.

For example, create a new layout file named Third_layout.xml with the following code:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >        <ButtonAndroid:id= "@+id/button_3"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/app_button_3"        /></LinearLayout>

Then create a new activity thirdactivity and inherit from the activities with the following code:

 package   Com.gui.activitytest;  import   android.app.Activity;  import   Android.os.Bundle;  import   Android.view.Window;  public  class  thirdactivity extends   Activity { protected  void   OnCreate (Bundle Savedinstancestate) { super  .oncreate (        Savedinstancestate);        Requestwindowfeature (Window.feature_no_title);    Setcontentview (r.layout.third_layout); }}

Finally register the thirdactivity in Androidmanifest.xml, the code is as follows:

<ActivityAndroid:name=". Thirdactivity " >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.VIEW"/>                <categoryAndroid:name= "Android.intent.category.DEFAULT" />                <DataAndroid:scheme= "http" />            </Intent-filter></Activity>

The above code, in Thirdactivity <intent-filter>, configures the action that the current activity can respond to is a constant value of Intent.action_view, and the category is the default value, <data >

The protocol that specifies the data through Android:scheme in the label must be the HTTP protocol, so that thirdactivity should be able to respond to the intent of a Web page in the same way as a browser.

Re-run the program and click on the button in the Firstactivity, as shown in the results:

As you can see from the picture, the system has moved from a list that shows all the programs that are currently able to respond to this intent.

Android Learning Notes (iv)

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.