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
- <Intent-filter>
- <Action Android: Name = "com. Manoel. secondactivity"/>
- <Category Android: Name = "android. Intent. Category. Default"/>
- </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
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
- Package = "com. Manoel. intents"
- Android: versioncode = "1"
- Android: versionname = "1.0" type = "codeph" text = "/codeph">
- <Uses-SDK Android: minsdkversion = "14"/>
- <Uses-Permission Android: Name = "android. Permission. call_phone"/>
- <Uses-Permission Android: Name = "android. Permission. Internet"/>
- <Application
- Android: icon = "@ drawable/ic_launcher"
- Android: Label = "@ string/app_name">
- <Activity
- Android: Label = "@ string/app_name"
- Android: Name = ". intentsactivity">
- <Intent-filter>
- <Action Android: Name = "android. Intent. Action. Main"/>
- <Category Android: Name = "android. Intent. Category. launcher"/>
- </Intent-filter>
- </Activity>
- <Activity Android: Name = ". mybrowseractivity"
- Android: Label = "@ string/app_name">
- <Intent-filter>
- <Action Android: Name = "android. Intent. Action. View"/>
- <Action Android: Name = "com. Manoel. mybrowser"/>
- <Category Android: Name = "android. Intent. Category. Default"/>
- <Data Android: Scheme = "HTTP"/>
- </Intent-filter>
- </Activity>
- </Application>
- </Manifest>
3. Add a button element to main. xml.[Java]View plaincopy
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: Orientation = "vertical">
- <Button
- Android: Id = "@ + ID/btn_launchmybrowser"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: onclick = "onclicklaunchmybrowser"
- Android: text = "launch my browser"/>
- </Linearlayout>
4. intentsactivity. Java[Java]View plaincopy
- Public class intentsactivity extends activity {
- /** Called when the activity is first created .*/
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- }
- Public void onclicklaunchmybrowser (view ){
- Intent I = new intent ("com. Manoel. mybrowser ");
- I. setdata (URI. parse ("http://www.amazon.com "));
- Startactivity (I );
- }
- }
5. browser. xml[Java]View plaincopy
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: Orientation = "vertical">
- <Webview
- Android: Id = "@ + ID/webview01"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"/>
- </Linearlayout>
6. myborwseractivity. Java[Java]View plaincopy
- Public class mybrowseractivity extends activity {
- /** Called when the activity is first created .*/
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. browser );
- Uri url = getintent (). getdata ();
- Webview = (webview) findviewbyid (R. Id. webview01 );
- Webview. setwebviewclient (New callback ());
- Webview. loadurl (URL. tostring ());
- }
- Private class callback extends webviewclient {
- @ Override
- Public Boolean shouldoverrideurlloading (webview view, string URL ){
- Return (false );
- }
- }
- }
7. debugging.
8. Click the "launch my browser" button to view a new activity and display the webpage of the Amazon.com website.