本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址 http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
我们已经知道,一个activity通过使用Intent对象调用另外一个activity。为了能让其他activity做出回应,还需要在AndroidManifest.xml中配置<intent-filter>元素,同时指定action和category。例如:
- <intent-filter>
- <actionandroid:name= "com.manoel.SecondActivity" />
- <categoryandroid:name= "android.intent.category.DEFAULT" />
- </intent-filter>
1. 新建一个工程,创建一个类:MyBrowserActivity.java。同时在res/layout中创建一个xml文件:brwoser.xml。
2. AndroidManifest.xml
- <?xmlversion= "1.0" encoding= "utf-8" ?>
- <manifestxmlns:android= "http://schemas.android.com/apk/res/android"
- package = "com.manoel.Intents"
- android:versionCode= "1"
- android:versionName= "1.0" >
- <uses-sdkandroid:minSdkVersion= "14" />
- <uses-permissionandroid:name= "android.permission.CALL_PHONE" />
- <uses-permissionandroid: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>
- <actionandroid:name= "android.intent.action.MAIN" />
- <categoryandroid:name= "android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activityandroid:name= ".MyBrowserActivity"
- android:label= "@string/app_name" >
- <intent-filter>
- <actionandroid:name= "android.intent.action.VIEW" />
- <actionandroid:name= "com.manoel.MyBrowser" />
- <categoryandroid:name= "android.intent.category.DEFAULT" />
- <dataandroid:scheme= "http" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- <?xmlversion= "1.0" encoding= "utf-8" ?>
- <LinearLayoutxmlns: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= "LaunchMyBrowser" />
- </LinearLayout>
- public class IntentsActivity extends Activity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- public void onCreate(BundlesavedInstanceState){
- super .onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- public void onClickLaunchMyBrowser(Viewview){
- Intenti= new Intent( "com.manoel.MyBrowser" );
- i.setData(Uri.parse( "http://www.amazon.com" ));
- startActivity(i);
- }
- }
- <?xmlversion= "1.0" encoding= "utf-8" ?>
- <LinearLayoutxmlns: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>
- public class MyBrowserActivity extends Activity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- public void onCreate(BundlesavedInstanceState){
- super .onCreate(savedInstanceState);
- setContentView(R.layout.browser);
- Uriurl=getIntent().getData();
- WebViewwebView=(WebView)findViewById(R.id.WebView01);
- webView.setWebViewClient( new Callback());
- webView.loadUrl(url.toString());
- }
- private class Callback extends WebViewClient{
- @Override
- public boolean shouldOverrideUrlLoading(WebViewview,Stringurl){
- return ( false );
- }
- }
- }
8. 点击“Launch my Browser”按钮,将会看到一个新的activity,并且显示了Amazon.com网站的网页。