Android intent-filter

本文深入解析了Intentfilter在AndroidManifest.xml中如何注册Activity、Service、Broadcastreceiver,详细阐述了Intentfilter的组成部分及其作用,包括action、category、data的使用方法,并通过代码实例展示了它们如何配合实现组件间的信使功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Intent filter是在Android的主配置文件AndroidManifest.xml中注册,主要用来指明Activity, Service, Broadcast reciver这三个组件可以响应哪些隐式intents。每个组件可以有一个或者多个intent filter。

(android四大组件分别为activity、service、content provider、broadcast receiver。)

  Intent filter有三个部分构成,分别是action(动作),data(数据)和category(类别)。下面通过代码分析每个部分的功能。
  

<intent-filter android:label="@string/Asen's blog">

  <action android:name="android.intent.action.MAIN" />

  <action android:name="android.intent.action.VIEW" />

  <action android:name="android.intent.action.EDIT" />

  <category android:name="android.intent.category.DEFAULT" />

  <category android:name="android.intent.category.LAUNCHER" />

  <category android:name="android.intent.category.ALTERNATIVE" />

  <data android:mimeType="video/mpeg" android:scheme="http".../>

  <data android:mimeType="audio/mpeg" android:content="com.example.project:200/folder/subfolder/etc"/>

  </intent-filter>

  每个action, category, data都是一行,如果有多个就写多行。

  另外,”android.intent.category.DEFAULT”这个category是用来指明组件是否可以接收到隐式Intents,所以说除了程序入口点这个filter不用包含DEFAULT category外,其余所有intent filter都要有这个category。

action
使用 android:name 特性来指定对响应的动作名。动作名必须是独一无二的字符串,所以,一个好的习惯是使用基于 Java 包的命名方式的命名系统。

category
使用 android:category 属性用来指定在什么样的环境下动作才被响应。每个 Intent Filter 标签可以包含多个 category 标签。你可以指定自定义的种类或使用 Android 提供的标准值

data
data 标签允许你指定组件能作用的数据的匹配;如果你的组件能处理多个的话,你可以包含多个条件。你可以使用下面属性的任意组合来指定组件支持的数据

data有两部分构成,一个是数据类型,另一个是URI。每个URI包括四个属性参数(scheme,host, port, path),形如:scheme://host:port/path举个例子 content://com.examplproject:e.200/folder/subfolder/etc 这个列子中scheme是content,host是com.examplproject,port是200,path是folder/subfolder/etcIntent filter和Intent相互配合,实现了Android系统四大组件之间的信使功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值