深入分析 Android Activity (二)

news/2024/9/23 4:50:40/

文章目录

      • 深入分析 Android Activity (二)
      • 1. `Activity` 的启动模式(Launch Modes)
        • 1.1 标准模式(standard)
        • 1.2 单顶模式(singleTop)
        • 1.3 单任务模式(singleTask)
        • 1.4 单实例模式(singleInstance)
      • 2. 深入理解 Intent 和 Intent Filters
        • 2.1 Intent
        • 2.2 Intent Filter
      • 3. Activity 的进程和线程模型
        • 3.1 进程
        • 3.2 线程
      • 4. Activity 与 Service 的交互
        • 4.1 启动服务
        • 4.2 绑定服务
      • 总结

深入分析 Android Activity (二)

1. Activity 的启动模式(Launch Modes)

Android 提供了几种不同的启动模式,用于定义 Activity 的启动行为。这些模式通过 AndroidManifest.xml 文件中的 android:launchMode 属性或 Intent 标志进行配置。

1.1 标准模式(standard)

这是默认的启动模式。每次启动 Activity 都会创建一个新的实例,无论该 Activity 是否已经存在于栈中。

<activity android:name=".MyActivity"android:launchMode="standard">
</activity>
1.2 单顶模式(singleTop)

如果当前任务的栈顶已经有该 Activity 实例,则重用该实例,并调用其 onNewIntent 方法,否则创建新的实例。

<activity android:name=".MyActivity"android:launchMode="singleTop">
</activity>

使用 Intent 标志也可以实现相同效果:

Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
1.3 单任务模式(singleTask)

在栈中只保留一个 Activity 实例。如果实例存在,则将其置于栈顶,并调用其 onNewIntent 方法,否则创建新的实例。

<activity android:name=".MyActivity"android:launchMode="singleTask">
</activity>
1.4 单实例模式(singleInstance)

创建一个单独的任务栈来管理该 Activity,并且在该任务栈中只存在这一个 Activity 实例。

<activity android:name=".MyActivity"android:launchMode="singleInstance">
</activity>

2. 深入理解 Intent 和 Intent Filters

2.1 Intent

Intent 是 Android 中用于在不同组件(如 ActivityServiceBroadcastReceiver)之间传递数据和请求操作的消息对象。主要分为两种类型:

  • 显式 Intent:明确指定目标组件的 Intent
  • 隐式 Intent:不指定目标组件,通过 Intent Filter 进行匹配。

显式 Intent 示例:

Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);

隐式 Intent 示例:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.example.com"));
startActivity(intent);
2.2 Intent Filter

Intent Filter 用于在 AndroidManifest.xml 中声明 ActivityServiceBroadcastReceiver 能够响应的 Intent 类型。

<activity android:name=".MyActivity"><intent-filter><action android:name="android.intent.action.VIEW"/><category android:name="android.intent.category.DEFAULT"/><data android:scheme="http" android:host="www.example.com"/></intent-filter>
</activity>

3. Activity 的进程和线程模型

3.1 进程

默认情况下,每个应用程序在独立的 Linux 进程中运行。应用程序中的所有组件(ActivityServiceBroadcastReceiverContentProvider)都在同一个进程中运行。

可以在 AndroidManifest.xml 中通过 android:process 属性为某些组件指定不同的进程:

<activity android:name=".MyActivity"android:process=":remote"/>
3.2 线程

Android 的主线程(也称为 UI 线程)用于处理 UI 更新和用户交互。因此,不能在主线程中执行耗时的操作,以避免阻塞 UI 响应。可以使用 AsyncTaskHandlerThreadExecutor 框架在后台线程中执行耗时操作。

使用 AsyncTask 进行后台操作:

private class DownloadTask extends AsyncTask<URL, Integer, Long> {protected Long doInBackground(URL... urls) {// Perform background task}protected void onProgressUpdate(Integer... progress) {// Update UI progress}protected void onPostExecute(Long result) {// Update UI with result}
}

4. Activity 与 Service 的交互

Service 是在后台运行的组件,用于执行长时间运行的操作。Activity 可以通过 startServicebindServiceService 交互。

4.1 启动服务

startService 用于启动服务:

Intent intent = new Intent(this, MyService.class);
startService(intent);
4.2 绑定服务

bindService 用于绑定服务,并获取 IBinder 以进行通信:

Intent intent = new Intent(this, MyService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

ServiceConnection 用于管理 Service 的连接和断开:

private ServiceConnection serviceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {MyService.LocalBinder binder = (MyService.LocalBinder) service;myService = binder.getService();isBound = true;}@Overridepublic void onServiceDisconnected(ComponentName name) {isBound = false;}
};

总结

Android Activity 的设计涉及多个方面,包括生命周期管理、启动模式、视图管理、进程和线程模型、以及与其他组件(如 FragmentService)的交互。理解 Activity 的设计原理和内部实现,有助于开发者构建高效、稳定和响应迅速的应用程序。通过深入分析和理解这些关键概念,开发者可以在实际项目中灵活应用这些知识,提升应用程序的用户体验和性能。

欢迎点赞|关注|收藏|评论,您的肯定是我创作的动力

在这里插入图片描述


http://www.ppmy.cn/news/1465589.html

相关文章

oracle sys无法远程访问问题解决

/myweb/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora sys的默认密码是manager&#xff0c;system的默认密码是change_on_install&#xff0c;为什么使用默认密码都无法正常登录呢&#xff1f;这就很奇怪了 解决&#xff1a; Winr——->Cmd——->输…

怎么提取pdf格式中的英语单词

思路&#xff1a; 第一步&#xff1a;适用python把需要导出的pdf文件单词导出到txt 第二步&#xff1a;把导出的txt导入到软件单词库&#xff0c;例如&#xff0c;金山词霸等软件内 第三步&#xff1a;熟练掌握以及删除单词库部分单词&#xff0c;达到对英文标准的单词记忆&…

C#中的数组探索

在C#编程语言中&#xff0c;数组是一种基本的数据结构&#xff0c;用于存储固定大小的同类型元素序列。本文将深入探讨C#数组的各个方面&#xff0c;包括定义、赋值、范围操作、切片、多维数组&#xff08;矩形与锯齿形&#xff09;、简化初始化表达式以及边界检查。 数组定义…

win_os_linux不能用于文件名的保留字符

windows 在 Windows 文件系统中&#xff0c;以下字符是保留字符&#xff0c;不能用于文件名或目录名&#xff1a; < (小于号)> (大于号): (冒号)" (双引号)/ (斜杠)\ (反斜杠)| (竖线)? (问号)* (星号) 此外&#xff0c;文件名不能以空格或句点 (.) 结尾&#x…

C语言 指针——指针变量做函数参数

目录 指针变量的解引用 为什么要用指针变量做函数参数&#xff1f; 演示Call by value 指针变量的解引用 为什么要用指针变量做函数参数&#xff1f; 演示Call by value

谷粒商城实战(030 业务-秒杀功能1)

Java项目《谷粒商城》架构师级Java项目实战&#xff0c;对标阿里P6-P7&#xff0c;全网最强 总时长 104:45:00 共408P 此文章包含第311p-第p314的内容 介绍 秒杀系统应该独立部署 8点开始秒杀 10点结束 秒杀价格 和 多少件&#xff08;库存&#xff09; 新建项目&a…

【Qt】深入探索Qt事件处理:从基础到高级自定义:QEvent

文章目录 前言&#xff1a;1. 事件的介绍2. 事件的处理2.1. 示例1&#xff1a; 重写鼠标进入和鼠标离开事件2.2. 示例2&#xff1a;当鼠标点击时&#xff0c;获取对应的坐标值&#xff1b;2.3. 鼠标释放事件2.4. 鼠标双击事件2.5. 鼠标移动事件2.6. 鼠标滚轮的滚动事件 3. 按键…

PyTorch训练关键点

1.背景 在网上找了一些资料用来训练关键点&#xff0c;一般都是人脸或者车牌关键点训练&#xff0c;或者是联合检测一起训练。很少有是单独基于轻量级网络训练单独关键点模型的工程&#xff0c;本文简单介绍一种简单方法和代码。 2.代码模块 &#xff08;1&#xff09;网络结…