我們已經知道,一個activity通過使用Intent對象調用另外一個activity。為了能讓其他activity做出回應,還需要在AndroidManifest.xml中配置<intent-filter>元素,同時指定action和category。例如:
[java] view plaincopy
- <intent-filter >
- <action android:name="com.manoel.SecondActivity" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
1. 建立一個工程,建立一個類:MyBrowserActivity.java。同時在res/layout中建立一個xml檔案:brwoser.xml。
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" >
-
- <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. 在main.xml中添加一個Button元素。[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 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 = (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網站的網頁。