我们已经知道,一个activity通过使用Intent对象调用另外一个activity。为了能让其他activity做出回应,还需要在AndroidManifest.xml中配置<intent-filter>元素,同时指定action和category。例如:
<intent-filter > <action android:name="net.learn2develop.SecondActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
1.新建一个工程,创建一个类:MyBrowserActivity.java。同时在res/layout中创建一个xml文件:brwoser.xml。
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" > <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.在main.xml中添加一个Button元素。
<?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.调试。
8.点击“Launch my Browser”按钮,将会看到一个新的activity,并且显示了Amazon.com网站的网页。
![](https://img-my.csdn.net/uploads/201203/30/1333111359_5818.jpg)