我们知道,实现android的Activity之间相互跳转需要用到Intent,

Intent又分为显式Intent和隐式Intent,

显式Intent很简单,比如我在FirstActivity中想跳转到SecondActivity,只需要直接声明就行了:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);

而在使用隐式Intent实现Activity之间跳转的时候,并没有明确指定要打开哪个activity,

而是通过指定3个参数:action,category,data,

然后让系统去寻找能够匹配得上这三个参数的Acativity,如果有多个符合条件的Activity,就会让用户选择其中一个打开。

例如我选择手机相册中的一张照片,点击“发送”按钮:

然后就可以让我选择是发送给QQ好友,微信好友还是发送到朋友圈。这实际上就是一个隐式Intent启动Activity的实例。

而决定一个Activity能够响应哪些Intent,就需要在AndroidManifest.xml的<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的action,category和data,比如说我在SecondActivity下设置如下代码:

<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MYACTION" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MYCATEGORY" />
</intent-filter>
</activity>

再在MainActivity中通过隐式Intent:

Intent intent=new Intent("android.intent.action.MYACTION");
startActivity(intent);

然后就启动了SecondActivity。

在这里,看似我们在Intent中只指定了要打开的活动只需要响应一个Action为“android.intent.action.MYACTION”就行,但是实际上,系统在使用隐式Intent的时候,会自动帮我们添加上“android.intent.category.default”,所以——实际上所有需要被隐式Intent启动的activity,都要加上<category android:name="android.intent.category.DEFAULT" />这一段声明,否则就会启动不了并提示无法匹配该Intent的错误:

如果你要隐式启动的那个活动是程序最先启动的那个activity,

即声明了<category android:name="android.intent.category.LAUNCHER" />,

就可以不用写<category android:name="android.intent.category.DEFAULT" />的声明了,

(在这里LAUNCHER,就是你打开程序,最先启动的那一个Activity。)另外,LAUNCHER一定要配合action MAIN一起使用,否则不会启动,即:

<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

android细节之android.intent.category.DEFAULT的使用的更多相关文章

  1. 什么时候加上android.intent.category.DEFAULT

    什么时候加上android.intent.category.DEFAULT 1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent ...

  2. android之android.intent.category.DEFAULT的用途和使用

    1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent. Explicit Intent明确的指定了要启动的Acitivity , ...

  3. android.intent.category.DEFAULT

    我们需要什么时候加android.intent.category.DEFAULT呢? 1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐式) intent什么是explicit(显示) i ...

  4. [转]理解android.intent.category.LAUNCHER 具体作用

    转自:http://blog.csdn.net/jackrex/article/details/9189657 android.intent.category.LAUNCHER 具体有什么作用?我做一 ...

  5. android.intent.category.BROWSABLE

    参考: http://blog.csdn.net/annkie/article/details/8349626 http://xiechengfa.iteye.com/blog/1004991 BRO ...

  6. android学习笔记29——Intent/IntentFilter

    Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...

  7. Android开发-API指南-Intent和Intent过滤器

    Intents and Intent Filters 英文原文:http://developer.android.com/guide/components/intents-filters.html 采 ...

  8. android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用

    你是不是很多时候,想从弹出的电话本姓名列表中中查找到某个人,然后再获取该人的详细信息呢? 你是不是想选择从弹出的列表中选择一张图片,然后将其进行进一步的操作呢? 如果,你想,那你是不是很像知道,我们应 ...

  9. Android组件的通讯——Intent

    转载:Android组件的通讯-Intent 1.概述 一个应用程序的三个核心组件——activities.services.broadcast receivers,都是通过叫做intents的消息激 ...

随机推荐

  1. React-router5.x 路由的使用及配置

    在 React router 中通常使用的组件有三种: 路由组件(作为根组件): BrowserRouter(history模式) 和 HashRouter(hash模式) 路径匹配组件: Route ...

  2. 爬虫之selenium模块;无头浏览器的使用

    一,案例 爬取站长素材中的图片:http://sc.chinaz.com/tupian/gudianmeinvtupian.html import requests from lxml import ...

  3. 转:spring mvc 设置@Scope("prototype")

    spring中bean的scope属性,有如下5种类型: singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例prototype表示每次获得bea ...

  4. Vagrant+VirtualBox虚拟环境

    Vagrant+VirtualBox虚拟环境 VagrantVirtualBox 软件安装 虚拟机基础配置 虚拟机创建 共享目录 配置网络 配置私有网络 配置公有网络 打包box与添加box 打包bo ...

  5. linux启动介绍

    1. linux内核3.0之前,使用init(初始化 )进程管理的启动程序.一旦升级到3.0(centos7)使用systemd的方式进行管理. 2. 启动模式:启动后执行哪些典型的操作.vi/etc ...

  6. 打造好用的C++ IDE环境

    https://www.jianshu.com/p/1aa989808e15 这哥们说的也是极好,也可以这部分直接看他的示例! mingw-w64应该可以算是mingw的改进版本吧,mingw系列编译 ...

  7. Tensorflow简单实践系列(二):张量

    在上一节中,我们安装 TensorFlow 并运行了最简单的应用,这节我们熟悉 TensorFlow 中的张量. 张量是 TensorFlow 的核心数据类型.数学里面也有张量的概念,但是 Tenso ...

  8. spring boot flyway 配置说明(摘抄)

    flyway.baseline-description对执行迁移时基准版本的描述. flyway.baseline-on-migrate当迁移时发现目标schema非空,而且带有没有元数据的表时,是否 ...

  9. 爬虫-requests用法

    中文文档 API: http://requests.kennethreitz.org/zh_CN/latest/ 安装 pip install requests 获取网页 # coding=utf-8 ...

  10. Goland在go mod vendor模式下无法识别某些库

    症状:go build可以正常编译,但代码编辑器里面提示找不到相关lib,后来发现是因为go.mod中没有用require这个库,补上库地址和版本.因为项目的mod vendor模式,版本一般不需要写 ...