摸鱼学Android 九(过滤器)

摸鱼学Android 九(过滤器)

过滤器(Filter)

1 说明

Android 操作系统使用过滤器来指定一系列活动、服务和广播接收器处理意图,需要借助于意图所指定的动作、类别、数据模式。
在 manifest 文件中使用 < intent-filter > 元素在活动,服务和广播接收器中列出对应的动作,类别和数据类型。

2 使用

当活动被过滤器所定义,其他活动就可以调用这个活动。
在调用活动之前,有一系列的 Android 检查测试:

  • 过滤器 < intent-filter > 需要列出一个或者多个的动作,不能为空;过滤器至少包含一个 元素,否则将阻塞所有的意图。如果多个动作被提到,Android 在调用活动前尝试匹配其中提到的一个动作。
  • 过滤器 < intent-filter > 可能列出0个,1个或者多个类别。如果没有类别被提到,Android 通过这个测试,如果有多个类别被提及,意图通过类型测试,每个意图对象的分类必须匹配过滤器中的一个分类。
  • 每个元素可以指定一个 URI 和一个数据类型(元媒体类型)。这里有独立的属性,如 URI 中的每个部分:模式,主机,端口和路径。意图包含有 URI 和类型,只有它的类型匹配了过滤器中列出的某个类型,则通过数据类型部分的测试。

3 实例

  1. MainActivity.java定义3个启动方法
	//通过view动作启动活动
    public void startWeb1(View view) {
        Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        startActivity(i);
    }
	
	//通过Launch动作启动活动,该活动仅仅捕获数据
    public void startWeb2(View view) {
        Intent i = new Intent("com.app.LAUNCH", Uri.parse("http://www.baidu.com"));
        startActivity(i);
    }

	//异常情况(匹配的数据,scheme部分设置为http)
    public void startWeb3(View view) {
        Intent i = new Intent("com.app.LAUNCH", Uri.parse("htt://www.baidu.com"));
        startActivity(i);
  1. 修改activity_main.java,创建3个按钮,分别绑定上面的方法
    <Button
            android:text="通过View动作启动浏览器"
            android:id="@+id/button"
            android:onClick="startWeb1"/>

    <Button
            android:text="通过Launch动作启动浏览器"
            android:id="@+id/button2"
            android:onClick="startWeb2"/>
            
    <Button
            android:text="异常情况"
            android:id="@+id/button3"
            android:onClick="startWeb3"/>
  1. 新建布局文件custom_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView android:id="@+id/show_data"
              android:layout_width="fill_parent"
              android:layout_height="400dp"/>
</LinearLayout>
  1. 添加CustomActivity.java,包含一个活动,可以被不同的意图调用。
public class CustomActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_view);
        //仅仅捕获数据
        TextView label = findViewById(R.id.show_data);
        Uri url = getIntent().getData();
        label.setText(url.toString());
    }
}
  1. 修改 AndroidManifest.xml 文件,添加 < intent-filter > 定义意图的规则来调用自定义活动。
	<activity android:name=".CustomActivity"
	         android:label="@string/app_name">
	
	   <intent-filter>
	       <action android:name="android.intent.action.VIEW" />
	       <action android:name="com.app.LAUNCH" />
	       <category android:name="android.intent.category.BROWSABLE" />
	       <category android:name="android.intent.category.DEFAULT" />
	       <data android:scheme="http" />
	   </intent-filter>
	</activity>
  1. 启动程序,分别点击3个按钮
    view:
    在这里插入图片描述
    Launch:
    在这里插入图片描述
    异常:程序闪退
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值