Android first line of code learning Note Five---implicit itent activities to start other programs

Source: Internet
Author: User

With implicit itent, not only can you start your own program activities, but you can also start the activities of other programs, which makes it possible to share functionality between multiple Android applications, such as when you need to display a Web page in your application, and you don't need to implement a browser yourself. Just call the system's browser to open the page. Modify the code of the Click button event in Firstactivity as follows:

Button.setonclicklistener (New View.onclicklistener () {public            void OnClick (view view) {                Intent intent=new Intent (Intent.action_view);                Intent.setdata (Uri.parse ("www.baidu.com"));                StartActivity (intent);            }        });

Here we first specify the Intent.action_view of the intent action, which is a built-in action of the Android system with a constant value of Android.intent.action.VIEW, Then, through the Uri.parse () method, a URL string is parsed into a URI object, and then the intent's SetData () method is called to pass the URI object in. The SetData () method receives a URI object that is used primarily to specify the data that is being manipulated by the current itent, which is usually generated as a string in the Uri.parse () method.

Rerun the program and click on the button to see the System browser open.

We can also configure a <data> tag in the <intent-filter> tab to more precisely specify what type of data the current activity responds to. <data> tags can mainly be configured with the following content.

Android:scheme is used for the protocol portion of the data, such as the HTTP section above.

Android:host is used to specify the hostname portion of the data, as in the www.baidu.com section of the previous example

The android:port is used to specify the port portion of the data, which is typically followed by the host name.

Android:path is used to specify the host name and the part after the port, such as the content after the domain name in a URL.

The android:mimetype is used to specify the type of data that can be processed, and is allowed to be specified using wildcard characters.

The current activity responds to intent only if the <data> tag is exactly the same as the data that is carried in the intent. However, in general, the <data> tag will not specify too much content, as the above example as long as the specified Android:scheme is HTTP can respond to the HTTP protocol intent.

Create an activity below and let it also respond to the intent that opens the page:

Right-click the com.example.firstactivity package->new->activity->empty Activity, create a new thirdactivity, tick generate Layout File, Layout file name thirdlayout,finish. Open Thirdlayout.xml Change to the following:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" match_parent "    android:layout_height= "Match_parent" >    <button        android:id= "@+id/button3"        android:layout_width= "Match_parent        " android:layout_height= "Wrap_content"        android:text= "Button3"/></linearlayout>

The code in Thirdactivity remains the same, and finally modifies the registration information in Androidmanifest.xml:

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

Configuring the action that the current activity responds to in thirdactivity <intent-filter> is a constant value of Intent.action_view, category specifies the default category value, and in the < Data> label the protocol portion of the data specified by Android:scheme must be an HTTP protocol, so that thirdactivity should be able to respond to a intent that opens a Web page just like a browser.

Note: Although we have declared that thirdactivity can respond to the intent of opening a webpage, the activity does not actually load and display the functionality of the Web page.

In addition to the HTTP protocol, you can specify additional protocols, the following code shows the calling system dial-up interface:

Button button = (Button) Findviewbyid (r.id.button1);        Button.setonclicklistener (new  View.onclicklistener () {            publicvoid OnClick (view view) {                Intent Intent=new  Intent (intent.action_dial);                Intent.setdata (Uri.parse ("tel:10086"));                StartActivity (intent);            }        });

First specify the Itent action is intent.action_dial, this is another Android system built-in action, and then in the data section to specify the protocol is tel, the number is 10086. Rerun the program as follows:

Android first line of code learning Note Five---implicit itent activities to start other programs

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.