Android中显式Intent和隐式Intent的区别

1、intent的中文名 称是意图,Intent是各个组件之间信息沟通的桥梁, 既能在Activity之间沟通,又能在Activity与Service之间沟通,也能在Activity与Broadcast之间沟通

复制代码
   **intent组成元素的列表说明**

2、显式Intent,直接指定来源活动与目标活动,属于精确匹配

1、在Intent的构造函数中指定

java 复制代码
Intent intent = new Intent(this, ActNextActivity.class); // 创建一个目标确定的意图

2、

java 复制代码
Intent intent = new Intent(); // 创建一个新意图
intent.setClass(this, ActNextActivity.class); // 设置意图要跳转的目标活动

3、

java 复制代码
Intent intent = new Intent(); // 创建一个新意图
// 创建包含目标活动在内的组件名称对象
ComponentName component = new ComponentName(this, ActNextActivity.class);
intent.setComponent(component); // 设置意图携带的组件信息

2、隐式intent ,没有明确指定要跳转的目标活动,只给出一个动作字符串让系统自动匹配,属于模糊匹配

一般来讲app都不希望对外部暴露活动的名称,所以先定义好有一个标记串,隐式的Intent便起到了标记过滤作用

调用系统拨号程序的代码例子,其中就用到了隐式intent 和 Uri:

java 复制代码
ActionUriActivity.java


String phoneNo = "12345";
Intent intent = new Intent(); // 创建一个新意图
intent.setAction(Intent.ACTION_DIAL); // 设置意图动作为准备拨号
Uri uri = Uri.parse("tel:" + phoneNo); // 声明一个拨号的Uri
intent.setData(uri); // 设置意图前往的路径
startActivity(intent); // 启动意图通往的活动页面

AndroidManifest.xml中也有intent过滤器的使用

不匹配的过滤,符合条件的优先显示,可以设置默认launcher首页

bash 复制代码
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
相关推荐
️学习的小王5 分钟前
Python + MySQL 学生信息管理系统实战教程
android·python·mysql
帅次11 分钟前
Android 高级工程师面试:Flutter 路由导航 近1年高频追问 18 题
android·flutter·面试
alexhilton9 小时前
MVI模式的完整历史、误解和现代Android范式
android·kotlin·android jetpack
心中有国也有家14 小时前
AtomGit Flutter 鸿蒙客户端:Canvas 绘制进阶-路径、渐变与混合模式
android·javascript·flutter·华为·harmonyos
hunterandroid15 小时前
WorkManager 可靠性实战:唯一任务、重试与幂等设计
android·前端
程序员老刘15 小时前
Android 17的10个AI功能,国内用户真正能用的有几个?
android·flutter·ai编程
花开彼岸天~17 小时前
鸿蒙应用开发实战【94】— 实战多卡号场景管理最佳实践
android·华为·harmonyos·鸿蒙系统
用户416596736935517 小时前
视频源文件音频轨异常分析记录
android
我命由我1234517 小时前
集成 mupdf-android 的示例做为模块使用报错,程序包android.support.v4.view不存在
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
雨白20 小时前
嵌套 ScrollView 滑动冲突实战:实现内部优先滚动
android