android系列:android.intent.category.DEFAULT的用途和使用

本文详细解释了Android系统中Intent的工作原理,包括显式Intent与隐式Intent的区别,以及如何通过action、category和data三个维度进行匹配。此外还介绍了IntentFilter在AndroidManifest.xml文件中的配置方法。

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

转自:http://blog.youkuaiyun.com/jason0539/article/details/10049899

我也该补补这些基础的知识了



1、要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent。

     Explicit Intent明确的指定了要启动的Acitivity ,比如以下Java代码:

[java]  view plain copy print ?
  1. Intent intent= new Intent(this, B.class);  

 Implicit Intent没有明确的指定要启动哪个Activity ,而是通过设置一些Intent Filter来让系统去筛选合适的Acitivity去启动。

2、intent到底发给哪个activity,需要进行三个匹配,一个是action,一个是category,一个是data。

理论上来说,如果intent不指定category,那么无论intent filter的内容是什么都应该是匹配的。但是,如果是implicit intent,android默认给加上一个CATEGORY_DEFAULT,这样的话如果intent filter中没有android.intent.category.DEFAULT这个category的话,匹配测试就会失败。所以,如果你的 activity支持接收implicit intent的话就一定要在intent filter中加入android.intent.category.DEFAULT。

例外情况是:

[java]  view plain copy print ?
  1. <intent-filter>  
  2.         <action android:name="android.intent.action.MAIN" />  
  3.         <category android:name="android.intent.category.LAUNCHER" />  
  4.     </intent-filter>  


中没有必要加入android.intent.category.DEFAULT,当然加入也没有问题。这个是应用启动默认的第一个启动的activity(每个应用有那么多activity,总得有一个是第一个启动的吧)

 

如果自己定义的某个Activity要通过隐式启动,在AndroidManifast.xm那么必须加上android.intent.category.DEFAULT,否则不起作用

除此之外,category的用途还有很多

比如做个桌面,按home键时启动自己做的应用

[html]  view plain copy print ?
  1. <activity  
  2.             android:name=".MainActivity"  
  3.             android:label="@string/app_name" >  
  4.             <intent-filter>  
  5.                 <action android:name="android.intent.action.MAIN" />  
  6.                 <category android:name="android.intent.category.LAUNCHER"/>  
  7.                 <category android:name="android.intent.category.HOME" />  
  8.             </intent-filter>  
  9.         </activity>  

在intent中是如何配置那三个匹配数据的呢,也简单一说

也就是说,在不直接指定要跳转的Activity的时候,为Intent提供一些相关的参数,让其自动去和AndroidManifest.xml中已有的Activity去匹配

IntentFilter在xml中的三個主要的参数:action,categary,data。

我们通过Intent的构造函数或者Intent提供的方法可以指定这个三个参数:

           

[java]  view plain copy print ?
  1. intent.setAction(action);  
  2. intent.setData(data);  
  3. intent.addCategory(category);  
打印台输出: 🔍 正在获取应用 com.baidu.searchbox 的所有权限... 🔧 [DEBUG] 执行命令: adb shell pm dump com.baidu.searchbox 🔧 [DEBUG] ADB 原始输出长度: 456188 字符 🔧 [DEBUG] 输出截取(前500字符): DUMP OF SERVICE package: Activity Resolver Table: Full MIME Types: application/x-zip-compressed: 7f4861d com.baidu.searchbox/.download.center.ui.DownloadedCategorySecActivity filter b682092 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Category: "android.intent.category.BROWSABLE" Scheme: "content" Scheme: "file" Path: "PatternMatcher{GLOB: .*\.zip}" Path: "PatternMatcher{GLOB: .*\.\w+\.zip}" Path: "PatternMatcher{GLOB: .*\.7z}" Path: "PatternMatcher{GLOB: .*\.\w+\.7z}" Path: "PatternMatcher{GLOB: .*\.rar}" Path: "PatternMatcher{GLOB: .*\.\w+\.rar}" StaticType: "application/zip" StaticType: "application/x-zip-compressed" StaticType: "application/x-7z-compressed" StaticType: "application/rar" StaticType: "application/x-rar-compressed" application/pdf: c611dca com.baidu.searchbox/.plugins.PDFViewerActivity filter a96b3b Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" StaticType: "application/pdf" application/rar: 7f4861d com.baidu.searchbox/.download.center.ui.DownloadedCategorySecActivity filter b682092 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Category: "android.intent.category.BROWSABLE" Scheme: "content" Scheme: "file" Path: "PatternMatcher{GLOB: .*\.zip}" Path: "PatternMatcher{GLOB: .*\.\w+\.zip}" Path: "PatternMatcher{GLOB: .*\.7z}" Path: "PatternMatcher{GLOB: .*\.\w+\.7z}" Path: "PatternMatcher{GLOB: .*\.rar}" Path: "PatternMatcher{GLOB: .*\.\w+\.rar}" StaticType: "application/zip" StaticType: "application/x-zip-compressed" StaticType: "application/x-7z-compressed" StaticType: "application/rar" StaticType: "application/x-rar-compressed" application/zip: 7f4861d com.baidu.searchbox/.download.center.ui.DownloadedCategorySecActivity filter b682092 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Category: "android.intent.category.BROWSABLE" Scheme: "content" Scheme: "file" Path: "PatternMatcher{GLOB: .*\.zip}" Path: "PatternMatcher{GLOB: .*\.\w+\.zip}" Path: "PatternMatcher{GLOB: .*\.7z}" Path: "PatternMatcher{GLOB: .*\.\w+\.7z}" Path: "PatternMatcher{GLOB: .*\.rar}" Path: "PatternMatcher{GLOB: .*\.\w+\.rar}" StaticType: "application/zip" StaticType: "application/x-zip-compressed" StaticType: "application/x-7z-compressed" StaticType: "application/rar" StaticType: "application/x-rar-compressed" application/vnd.openxmlformats-officedocument.presentationml.presentation: 170d258 com.baidu.searchbox/.plugins.PPTViewerActivity filter 55b4c96 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Scheme: "file" Scheme: "content" Path: "PatternMatcher{GLOB: .*\.pptx}" StaticType: "application/vnd.openxmlformats-officedocument.presentationml.presentation" application/vnd.ms-powerpoint: 170d258 com.baidu.searchbox/.plugins.PPTViewerActivity filter 5308fb1 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Scheme: "file" Scheme: "content" Path: "PatternMatcher{GLOB: .*\.ppt}" StaticType: "application/vnd.ms-powerpoint" application/vnd.openxmlformats-officedocument.wordprocessingml.document: b398017 com.baidu.searchbox/.plugins.DOCViewerActivity filter 20d75ed Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Scheme: "file" Scheme: "content" Path: "PatternMatcher{GLOB: .*\.docx}" StaticType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" audio/*: b35adee com.baidu.searchbox/.systemshare.SystemShareToNetDisk filter 4edc28f Action: "android.intent.action.SEND" Category: "android.intent.category.DEFAULT" StaticType: "image" StaticType: "video" StaticType: "audio" mPriority=0, mOrder=0, mHasStaticPartialTypes=true, mHasDynamicPartialTypes=false application/vnd.ms-excel: bf79c22 com.baidu.searchbox/.plugins.EXCELViewerActivity filter e4c06b3 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Scheme: "file" Scheme: "content" Path: "PatternMatcher{GLOB: .*\.xls}" StaticType: "application/vnd.ms-excel" application/x-rar-c... 🔧 [DEBUG] 找到 0 个权限声明段落 🔧 [DEBUG] 总共找到 0 个有效权限 ⚠️ dumpsys 未找到权限,尝试解析 APK 中的 AndroidManifest.xml... ❌ 未找到任何权限声明,请检查 ADB 输出或手动提供权限列表
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值