【Android 开发教程】使用Intent-Filter
来源:互联网 发布:台湾手机网络制式 编辑:程序博客网 时间:2024/05/18 09:10
本章节翻译自《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 >
- <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
- <?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>
- <?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>
- 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);
- }
- }
- <?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>
- 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);
- }
- }
- }
8. 点击“Launch my Browser”按钮,将会看到一个新的activity,并且显示了Amazon.com网站的网页。
- 【Android 开发教程】使用Intent-Filter
- Android开发教程:理解Intent和Intent Filter
- Android开发教程:理解Intent和Intent Filter
- Android Intent开发 Intent-filter
- 【Android 开发教程】解决Intent-Filter的冲突
- Android开发之Intent Filter
- android开发——intent/intent-filter
- 【Android 开发教程】使用Intent传递数据
- 【Android 开发教程】使用Intent发送短信
- Android的Intent与Intent Filter使用
- Android Intent Filter简单使用
- Android Intent Filter简单使用
- Android Intent Filter简单使用
- Android中的Intent filter使用
- Android 程式开发:(四)使用Intent调用“内置”应用 —— 4.3配置Intent-Filter
- Android intent intent-filter
- Android intent intent-filter
- Android intent intent-filter
- java基本数据类型及其封装类
- VC6.0学习:【001】在系统菜单中添加菜单项
- HTML字符转码
- 【Android 开发教程】理解Intent对象
- hdu 2050 折线分割平面
- 【Android 开发教程】使用Intent-Filter
- 【Android 开发教程】添加Category
- Hadoop中mapred包和mapreduce包的区别
- TortoiseSVN提交文件时'svn/!svn/me'path not found的解决
- C++统计字符串中(文件中)单词个数
- 【Android 开发教程】动态添加Fragments
- 解析网页源代码中的115网盘链接
- Hadoop动态添加删除datanode及tasktracker
- 【Android 开发教程】Fragment的生命周期