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:
<Intent-filter>
<Action android: name = "net. learn2develop. 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
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "net. learn2develop. 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 = "net. learn2develop. 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. <? 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 package net. learn2develop. Intents;
Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
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 view ){
Intent I = new Intent ("net. learn2develop. MyBrowser ");
I. setData (Uri. parse ("http://www.amazon.com "));
StartActivity (I );
}
}
5. browser. xml <? 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 package net. learn2develop. Intents;
Import android. app. Activity;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. webkit. WebView;
Import android. webkit. WebViewClient;
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 = (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.
From manoel's column