`

“android.intent.category.DEFAULT”的目的是什么?

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

Explicit Intent明确的指定了要启动的Acitivity ,比如以下Java代码:
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

例外情况是:android.intent.category.MAIN和android.intent.category.LAUNCHER的filter中没有必要加入android.intent.category.DEFAULT,当然加入也没有问题。

我们定义的activity如果接受implicit intent的话,intent filer就一定要加上android.intent.category.DEFAULT这个category


来自另一篇文章的解释:

在写 AndroidManifest.xml 的时候,一直没有搞明白,什么时候要给 Activityandroid.intent.category.DEFAULT 过滤器,现在才明白。

--------------------------------------------------------------------------------
Android treats all implicit intents passed to startActivity() as if they contained at least one category: "android.intent.category.DEFAULT" (the CATEGORY_DEFAULT constant). Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters
--------------------------------------------------------------------------------

意思是说,每一个通过 startActivity() 方法发出的隐式 Intent 都至少有一个 category,就是 "android.intent.category.DEFAULT",所以只要是想接收一个隐式 Intent 的 Activity 都应该包括 "android.intent.category.DEFAULT" category,不然将导致 Intent 匹配失败。

从上面的论述还可以获得以下信息:
1、一个 Intent 可以有多个 category,但至少会有一个,也是默认的一个 category
2、只有 Intent 的所有 category 都匹配上,Activity 才会接收这个 Intent


最新历史版本 :Android中的category大全 -- 未审核

  • 编辑时间:06-15 15:14 历史版本编辑者:vikiviki 历史版本:
  • 内容长度:8998 图片数:0目录数:0
  • 修改原因:

Api Level 3(SDK 1.5)和Api Level 4(SDK 1.6):

Xml代码 
  1. android.intent.category.ALTERNATIVE  
  2. android.intent.category.BROWSABLE  
  3. android.intent.category.DEFAULT  
  4. android.intent.category.DEVELOPMENT_PREFERENCE  
  5. android.intent.category.EMBED  
  6. android.intent.category.HOME  
  7. android.intent.category.INFO  
  8. android.intent.category.LAUNCHER  
  9. android.intent.category.MONKEY  
  10. android.intent.category.OPENABLE  
  11. android.intent.category.PREFERENCE  
  12. android.intent.category.SELECTED_ALTERNATIVE  
  13. android.intent.category.TAB  

 Api Level 5(SDK 2.0)增加的:

Xml代码 
  1. android.intent.category.CAR_DOCK  
  2. android.intent.category.DESK_DOCK  

Api Level 6和7没有变化。

 Api Level 8(SDK 2.2)增加的:

Xml代码 
  1. android.intent.category.CAR_MODE

 

 

分享到:
评论

相关推荐

    android 8.1设置添加Ethernet功能选项

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

    打开附件.java

    intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/myFile/"+name )); ...

    Google Android揭秘第三章实例.rar

    android:name="android.intent.category.DEFAULT" /> android:name="com.msi.manning.restaurant.VIEW_LIST" /> </intent-filter> <activity android:name="ReviewDetail" android:label...

    点击浏览器中的URL链接,启动特定的App。

    <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" android:host="jp.app" android:pathPrefix="/...

    android 8.1设置添加Ethernet功能选项patch文件

    + <category android:name="android.intent.category.DEFAULT" /> + <category android:name="android.intent.category.VOICE_LAUNCH" /> + <category android:name="com.android.settings.SHORTCUT" /> + </intent-...

    Android功能代码

    1.打开设置主界面 Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);...intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("vnd.android-dir/mms-sms"); startActivity(intent);

    android实现打电话功能

    解压就可运行。public class PhoneActicity extends Activity { private EditText mobileText; @Override public void onCreate(Bundle savedInstanceState) { ...//内部会添加android.intent.category.DEFAULT

    基于Android CALL SendMes Test的相关介绍

    intent.addCategory(“android.intent.category.DEFAULT”);intent.setData(Uri.parse(“tel:” + strmobile));startActivity(intent);//方法内部会自动为Intent添加类别:android.intent.category.DEFAULT 短信发送...

    ContentProvider

    <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <provider android:name=".provider" android:authorities="com.example.tigongzhe.provider" android:multiprocess=...

    adb1.0.26包含fastboot.exe

    ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具,也是 Android 设备玩家的好玩具。 注:有部分命令的支持情况可能与 Android 系统版本及定制 ROM 的实现有关。 基本用法 命令语法 ...

    Android 调用第三方导航(百度、高德、腾讯)以及坐标转换

    intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); double[] gd_lat_lon ; if(!RoutingXModel.isGpslatlon){ gd_lat_lon= bdToGaoDe(xModel.poc_lat,xModel.poc_...

    MapView的使用

    在论坛里看到一篇 "MapView和其它控件一起显示 " 的帖子, 那是很老的一篇帖子了, 很多朋友都说无法在android SDK 1.0上运行。既然那么多人关心,我在这里就把它重写一遍,顺便加入了一些新的功能 ,感兴趣的朋友可以...

    可扩展的文件选择器NoNonsense-FilePicker

    <category android:name="android.intent.category.DEFAULT"/> </intent-filter> 在java代码中调用选择器: // This always works Intent i = newIntent(context, FilePickerActivity.class); // This works if you...

    android教程之把自己的应用加入到系统分享中

    代码如下:<intent> <action android:name=”android.intent.action.SEND”> <data android:mimeType=”image/*”/> <category android:name=”android.intent.category.DEFAULT”></intent> 您可能感兴趣的

    Android 自定义相机及分析

    Android 自定义相机及分析源码 使用Android 系统相机的方法: ... <category android:name=android.intent.category.DEFAULT> </intent> action的作用就是声明action的类型,便于Intent的使用,cate

Global site tag (gtag.js) - Google Analytics