Android之路(4)——Intent 与 intent-filter

Intent 与 intent-filter简介

  • Intent
    Intent并不是Android应用地组件,但它对Android应用的作用十分大——它是Android组件之间通信的载体。Intent封装了当前组件需要启动或触发的目标组件的信息,也可以封装数据来进行传递。
  • intent-filter
    AndroidManifest.xml中用于配置组件具体属性的一个标签

Intent的属性以及intent-filter标签里的配置

1. Component属性
指定Component属性的Intent已经明确了他将要启动那个组件,这种Intent被称之为显示Intent;
指定Component属性的Intent被称之为隐式Intent,他会根据Intent指定的规则去启动符合条件的组件;
如下面两种显示启动其他Activity的方法时等价的:

 		//法一:通过Component属性
        ComponentName componentName = new ComponentName(MainActivity.this,FirstActivity.class);
        Intent intent = new Intent();
        intent.setComponent(componentName);
        startActivity(intent);
        
        //法二:通常写法
        Intent intent = new Intent(MainActivity.this,FirstActivity.class);
        startActivity(intent);

2. Action 与 Category 属性
Intent的Action和Category属性都是一个普通的字符串。
Action表示该Intent所要完成的一个“抽象”动作,具体这个动作由那个组件(可以是Activity也可以是BroadcastReceiver)来完成,Action这个字段并不管。
Category则用于为Action增加额外的附加信息。

			<!--主活动所声明的Action和Category属性-->
 			<intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

具体使用参考Activity中的使用:
https://blog.csdn.net/qq_43284141/article/details/101618167
注意:一个Intent对象最多只能包括一个Action属性,程序可调用Intent的setAction(String str)方法开设置Action属性值;但一个Intent对象可以包括多个Category属性。

系统自带的Action和Category值:
在这里插入图片描述
在这里插入图片描述

3 . Data 和 Type属性

  • Data属性通常用于向Action属性提供操作的数据。Dara属性接受一个Uri对象(Uri格式:scheme://host:post/path)
  • Type属性用于指定该Data属性所指定Uri的MIME类型,这种MIME类型的格式为:abc/xyz

Data属性和Type属性的关系比较微妙,它们会相互覆盖:

 		//Data 覆盖 Type
        Intent intent = new Intent();
        intent.setType("abc/xyz");
        intent.setData(Uri.parse("http://www.baidu.com"));

        //Type覆盖Data
        Intent intent = new Intent();
        intent.setData(Uri.parse("http://www.baidu.com"));
        intent.setType("abc/xyz");
        
        //希望既有Data 又有 Type
        Intent intent = new Intent();
        intent.setDataAndType(Uri.parse("http://www.baidu.com"),"abc/xyz");

AndroidManifest.xml中配置

 	<intent-filter>
                <action android:name="android.intent.action.FIRSTACTIVITY"/>
                <category android:name="android.intent,category.FIRSTACTIVITY_CATEGORY"/>
                <category android:name="android.intent,category.DEFAULT"/>
                <data 
                	android:mimeType="abc/xyz"
                    android:scheme="content"
                    android:host="com.andriod.contacts"
                    android:port="8888"  //可以不写  也要看具体的端口号
                    android:path="/contacts"
                    android:pathPrefix=""  // 一般不写
                    android:pathPattern=""  //一般不写
                    />
   </intent-filter>

使用Action和Data属性启动系统Activity
常见组合:
ACTION_VIEW content://com.android.contacts/contacts/1 显示标识为1的联系人信息
ACTION_EDIT content://com.android.contacts/contacts/1 编辑标识为1的联系人信息
ACTION_VIEW content://contacts/people/ 显示所有联系人的列表
ACTION_VIEW http:www.baidu.com 使用手机里的浏览器打开一个网页
ACTION_VIEW tel:10086 显示向指定号码10086拨号的界面

				//使用手机里的浏览器打开一个网页
 				Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
                
                //显示联系人的列表
				Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("content://com.android.contacts/contacts"));
                startActivity(intent);

				//显示打电话界面
				Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4. Extra属性
通常用于在多个Action之间进行数据交换
Intent的Extra属性应该是一个Bundle对象

5.Flag属性
通常用于为该Intent添加一些额外的控制旗标,Intent可调用 addFlag() 方法来添加控制旗标。
是在通过这些旗标启动其他组件时
FLAG_ACTIVITY_BROUGHT_TO_FRONT :若Activity已存在,直接将其带到前台
FLAG_ACTIVITY_CLEAR_TOP :将要启动的Activity之上的Activity全部弹出
FLAG_ACTIVITY_NEW_TASK : 默认,新建一个Activity
FLAG_ACTIVITY_NO_HISTORY : 使用该方法启动的Activity不会留在栈中
FLAG_ACTIVITY_SINGLE_TOP : 类似于singleTop加载模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值