android细节之android.intent.category.DEFAULT的使用
我们知道,实现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的使用的更多相关文章
- 什么时候加上android.intent.category.DEFAULT
什么时候加上android.intent.category.DEFAULT 1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent ...
- android之android.intent.category.DEFAULT的用途和使用
1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent. Explicit Intent明确的指定了要启动的Acitivity , ...
- android.intent.category.DEFAULT
我们需要什么时候加android.intent.category.DEFAULT呢? 1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐式) intent什么是explicit(显示) i ...
- [转]理解android.intent.category.LAUNCHER 具体作用
转自:http://blog.csdn.net/jackrex/article/details/9189657 android.intent.category.LAUNCHER 具体有什么作用?我做一 ...
- android.intent.category.BROWSABLE
参考: http://blog.csdn.net/annkie/article/details/8349626 http://xiechengfa.iteye.com/blog/1004991 BRO ...
- android学习笔记29——Intent/IntentFilter
Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...
- Android开发-API指南-Intent和Intent过滤器
Intents and Intent Filters 英文原文:http://developer.android.com/guide/components/intents-filters.html 采 ...
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
你是不是很多时候,想从弹出的电话本姓名列表中中查找到某个人,然后再获取该人的详细信息呢? 你是不是想选择从弹出的列表中选择一张图片,然后将其进行进一步的操作呢? 如果,你想,那你是不是很像知道,我们应 ...
- Android组件的通讯——Intent
转载:Android组件的通讯-Intent 1.概述 一个应用程序的三个核心组件——activities.services.broadcast receivers,都是通过叫做intents的消息激 ...
随机推荐
- 下载文件时-修改文件名字 Redis在Windows中安装方法 SVN安装和使用(简单版) WinForm-SQL查询避免UI卡死 Asp.Net MVC Https设置
下载文件时-修改文件名字 1后台代码 /// <summary> /// 文件下载2 /// </summary> /// <param name="Fil ...
- 利用 AWS DMS 在线迁移 MongoDB 到 Amazon Aurora
将数据从一种数据库迁移到另一种数据库通常都非常具有挑战性,特别是考虑到数据一致性.应用停机时间.以及源和目标数据库在设计上的差异性等因素.这个过程中,运维人员通常都希望借助于专门的数据迁移(复制)工具 ...
- Springboot+Mybatis的逆向工程
Mybatis逆向工程,自动生成 entity类和常用的增删改查方法. 1.pom.xml引入类 <!-- 通用mapper 用于mabatis封装的基础增删改查的功能--><dep ...
- PAT 乙级 1006.换个格式输出整数 C++/Java
1006 换个格式输出整数 (15 分) 题目来源 让我们用字母 B 来表示“百”.字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(n < 1000),换个格式来输出任一个不 ...
- 配置VScode c语言环境
vscode 提示 <sys/socket.h>找不到, 原来是不同平台上,头文件不一样. 参考:https://blog.csdn.net/qq_28581077/article/det ...
- LGOJP3193 [HNOI2008]GT考试
\(f[i][j]\)表示当前摆放到第\(i\)位,然后当前的匹配长度为\(j\) \(f[i][j]=\sum {f[i][k]*g[k][j]}\) \(g[i][j]\)表示将长度为\(i\)的 ...
- hive中时间操作(二)
转:https://blog.csdn.net/qq646748739/article/details/77997276 --Hive中日期函数总结:--1.时间戳函数--日期转时间戳:从1970-0 ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- 干货,不小心执行了rm -f,除了跑路,如何恢复?https://www.cnblogs.com/justmine/p/10359186.html
前言 每当我们在生产环境服务器上执行rm命令时,总是提心吊胆的,因为一不小心执行了误删,然后就要准备跑路了,毕竟人不是机器,更何况机器也有bug,呵呵. 那么如果真的删除了不该删除的文件,比如数据库. ...
- luogu 2742 二维凸包
链接 luogu 模板一 上下利用斜率求凸包然后合并. #include <bits/stdc++.h> using namespace std; const int N=10005; c ...