Android 优化之app启动优化的实现
作者:七适散人 时间:2023-10-11 07:31:22
App 启动方式
冷启动
App 没有启动过或 App 进程被杀,系统中不存在该 App 进程,此时启动即为冷启动。需要创建 App 进程,加载相关资源,启动 Main Thread,初始化首屏 Activity 等。在这个过程中,屏幕会显示一个空白的窗口(颜色基于主题),直至首屏 Activity 完全启动。
热启动
热启动意味着 App 进程只是处于后台,系统只是将其从后台带到前台展示给用户。类同与冷启动,在这个过程中,屏幕会显示一个空白的窗口(颜色基于主题),直至 activity 渲染完毕。
温启动
介于冷启动和热启动之间,一般来说在以下两种情况下发生:
用户返回退出了 App,然后又启动。进程可能还在运行,但是 activity 需要重建。
用户退出 App 后,系统可能由于内存原因将 App 杀死,进程和 activity 都需要重启,但是可以在 onCreate 中将被动杀死锁保存的状态(saved instance state)恢复。
启动时间检测
adb
adb shell am start -W
[应用报名]/[Activity的全路径名],得到三个时间值:
1、ThisTime
一般和 TotalTime 相同,如果应用启动开启了一个过渡用的全透明页面预处理一些事情,这样会比 TotalTime 小。
2、TotalTime
应用启动时间,包括创建进程、Application 初始化、Activity 初始化到显示。
3、WaitTime
一般比 TotalTime 大,包括系统的耗时。
代码打点
Application 的 attachBaseContext 在应用进程首先创建时调用,可做冷启动的开始打点。
优化
1、布局优化
2、逻辑优化
必要且耗时的逻辑,考虑单独开线程执行
必要不耗时,按优先级高低依次执行
非必要的延迟初始化,等用到再初始化
3、针对冷启动的欺骗效果
使用 placeholder UI
Android 最新的 Material Design 建议使用一个 placeholder UI 来展示给用户直至 App 加载完毕,类似 iOS 的做法。
自定义主题,设置 windowBackground 属性,给 Window 加上背景。
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/logo_splash</item>
</style>
先加载一个不渲染布局的 Activity 作为启动屏
写一个什么都不做的 LogoSplashActivity
public class LogoSplashActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 这里并没有 setContentView,单纯只是用来跳转到相应的 Activity
// 目的是减少首屏渲染
if (AppPref.isFirstRunning(this)) {
IntroduceActivity.launch(this);
} else {
MainActivity.launch(this);
}
finish();
}
}
在 AndroidManifest.xml 中设置其为启动屏,并加上主题
<activity
android:name=".ui.module.main.LogoSplashActivity"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
这样启动屏显示 LogoSplashActivity,本身不渲染布局,显示主题设置的 placehold UI 背景。
来源:https://www.jianshu.com/p/8993d6bdc6c7
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis-Plus 查询返回实体对象还是map
Android获取手机通讯录、sim卡联系人及调用拨号界面方法
Android自定义ActionBar实例
![](https://img.aspxhome.com/file/2023/3/137393_0s.png)
Spring Boot+Mybatis的整合过程
![](https://img.aspxhome.com/file/2023/7/84317_0s.png)
Unity使用鼠标旋转物体效果
![](https://img.aspxhome.com/file/2023/6/68616_0s.png)
Android HTTP网络请求的异步实现
![](https://img.aspxhome.com/file/2023/6/117076_0s.jpg)
基于C#实现图片合成功能
![](https://img.aspxhome.com/file/2023/8/89808_0s.png)
Java生成动态版验证码的方法实例
Activiti7整合Springboot使用记录
![](https://img.aspxhome.com/file/2023/7/91147_0s.jpg)
IntelliJ IDEA Run时报“无效的源发行版:16“错误问题及解决方法
![](https://img.aspxhome.com/file/2023/5/62455_0s.png)
Java中判断对象是否为空的方法的详解
java 类加载机制和反射详解及实例代码
![](https://img.aspxhome.com/file/2023/5/68745_0s.png)
C#实现只运行单个实例应用程序的方法(使用VB.Net的IsSingleInstance)
Java毕业设计实战之药店信息管理系统的实现
![](https://img.aspxhome.com/file/2023/2/72522_0s.jpg)
使用C#实现阿拉伯数字到大写中文的转换
![](https://img.aspxhome.com/file/2023/3/122653_0s.jpg)
C#使用NPOI设置Excel下拉选项
Java编程实现帕斯卡三角形代码示例
![](https://img.aspxhome.com/file/2023/3/58303_0s.gif)
21天学习android开发教程之SurfaceView
![](https://img.aspxhome.com/file/2023/4/116044_0s.jpg)
C#实现XML文件读取
关于Android中自定义ClassLoader耗时问题的追查
![](https://img.aspxhome.com/file/2023/5/137985_0s.jpg)