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>
相关推荐
低调小一11 小时前
Android传统开发 vs Android Compose vs HarmonyOS ArkUI 对照表
android·华为·harmonyos
雨白11 小时前
Java 多线程指南:从基础用法到线程安全
android·java
00后程序员张12 小时前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
程序员江同学14 小时前
ovCompose + AI 开发跨三端的 Now in Kotlin App
android·kotlin·harmonyos
2501_9151063214 小时前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
消失的旧时光-194314 小时前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
灿烂阳光g15 小时前
SELinux 策略文件编写
android·linux
.豆鲨包15 小时前
【Android】Viewpager2实现无限轮播图
android·java
xiangxiongfly91515 小时前
Android CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout实现折叠置顶效果
android·折叠