Android Program Development: (2) use intent -- 2.7 use intent-Filter

Source: Internet
Author: User

We already know that an activity calls another activity by using an intent object. To allow other activities to respond, you also need to configure the <intent-filter> element in androidmanifest. xml and specify action and category. For example:

[Java]View plaincopy
  1. <Intent-filter>
  2. <Action Android: Name = "com. Manoel. secondactivity"/>
  3. <Category Android: Name = "android. Intent. Category. Default"/>
  4. </Intent-filter>

1. Create a project and create a class: mybrowseractivity. java. Create an XML file brwoser. XML in RES/layout.

2. androidmanifest. xml

[Java]View plaincopy
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "com. Manoel. intents"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  6. <Uses-SDK Android: minsdkversion = "14"/>
  7. <Uses-Permission Android: Name = "android. Permission. call_phone"/>
  8. <Uses-Permission Android: Name = "android. Permission. Internet"/>
  9. <Application
  10. Android: icon = "@ drawable/ic_launcher"
  11. Android: Label = "@ string/app_name">
  12. <Activity
  13. Android: Label = "@ string/app_name"
  14. Android: Name = ". intentsactivity">
  15. <Intent-filter>
  16. <Action Android: Name = "android. Intent. Action. Main"/>
  17. <Category Android: Name = "android. Intent. Category. launcher"/>
  18. </Intent-filter>
  19. </Activity>
  20. <Activity Android: Name = ". mybrowseractivity"
  21. Android: Label = "@ string/app_name">
  22. <Intent-filter>
  23. <Action Android: Name = "android. Intent. Action. View"/>
  24. <Action Android: Name = "com. Manoel. mybrowser"/>
  25. <Category Android: Name = "android. Intent. Category. Default"/>
  26. <Data Android: Scheme = "HTTP"/>
  27. </Intent-filter>
  28. </Activity>
  29. </Application>
  30. </Manifest>

3. Add a button element to main. xml.[Java]View plaincopy

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: Orientation = "vertical">
  6. <Button
  7. Android: Id = "@ + ID/btn_launchmybrowser"
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "wrap_content"
  10. Android: onclick = "onclicklaunchmybrowser"
  11. Android: text = "launch my browser"/>
  12. </Linearlayout>

4. intentsactivity. Java[Java]View plaincopy

  1. Public class intentsactivity extends activity {
  2. /** Called when the activity is first created .*/
  3. @ Override
  4. Public void oncreate (bundle savedinstancestate ){
  5. Super. oncreate (savedinstancestate );
  6. Setcontentview (R. layout. Main );
  7. }
  8. Public void onclicklaunchmybrowser (view ){
  9. Intent I = new intent ("com. Manoel. mybrowser ");
  10. I. setdata (URI. parse ("http://www.amazon.com "));
  11. Startactivity (I );
  12. }
  13. }

5. browser. xml[Java]View plaincopy

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: Orientation = "vertical">
  6. <Webview
  7. Android: Id = "@ + ID/webview01"
  8. Android: layout_width = "wrap_content"
  9. Android: layout_height = "wrap_content"/>
  10. </Linearlayout>

6. myborwseractivity. Java[Java]View plaincopy

  1. Public class mybrowseractivity extends activity {
  2. /** Called when the activity is first created .*/
  3. @ Override
  4. Public void oncreate (bundle savedinstancestate ){
  5. Super. oncreate (savedinstancestate );
  6. Setcontentview (R. layout. browser );
  7. Uri url = getintent (). getdata ();
  8. Webview = (webview) findviewbyid (R. Id. webview01 );
  9. Webview. setwebviewclient (New callback ());
  10. Webview. loadurl (URL. tostring ());
  11. }
  12. Private class callback extends webviewclient {
  13. @ Override
  14. Public Boolean shouldoverrideurlloading (webview view, string URL ){
  15. Return (false );
  16. }
  17. }
  18. }

7. debugging.

8. Click the "launch my browser" button to view a new activity and display the webpage of the Amazon.com website.

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.