Android实战APP启动速度优化
作者:laozhang 时间:2023-03-21 15:34:18
APP启动速度非常重要,APP启动速度慢,可能会造成用户体验不良好,尤其是在最近用Android studio之后,如果长时间不打开app,启动速度就会特别的慢,下面我们一起探讨一下影响app启动速度的原因,以及解决方案。
检测启动时间
首先我们要知道app的启动时间,然后你也可以凭着感觉来,这里我教大家一个装逼的方法:
adb shell am start -W [packageName]/[.MainActivity]
用adb命令可以检测启动时间,示例如下:
./adb shell am start -W "com.hchstudio.dict"/".MainActivity"
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.hchstudio.dict/.MainActivity }
Status: ok
Activity: com.hchstudio.dict/.MainActivity
ThisTime: 7524
TotalTime: 7524
WaitTime: 7551
Complete
com.hchstudio.dict为应用包名
.MainActivity为所要启动的主页面函数
WaitTime为我们所关注的启动时间
那么7551算不算启动时间长呢?答案是很长,。。。这是一个很长时间没有启动的app,当它第一次启动的时候就有很长的启动时间,下面来说怎么来减少吧。
减少应用启动耗时
app的启动流程,简单的说,主要包括两部分:
Application
启动界面的onCreate方法
因此最简单的方法就是尽量减少这两个方法中的耗时操作。
上面的方法从代码上解决了启动速度,但是用户启动的时候还是感觉慢,怎么办呢?直接上代码:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorAccent</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Launcher">
<item name="android:windowBackground">@color/colorLauncher</item>
</style>
这是android style文件,其中AppTheme.Launcher为启动界面的主题样式,给你的app首页主题样式加上android:windowBackground,放一下app的背景图片,这样即使app启动慢,也会首先加载背景,这样就会给用户造成一种假象,认为是app已经启动。
Instant Run
对于Android studio的新功能Instant Run,你知道多少呢?
Instant Run也是影响启动的因素之一,不过这个在发布的版本里是没有的,所以小伙伴们不要担心。
标签:Android,APP,启动速度
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#如何优雅的结束线程
2022-07-27 19:45:27
Java毕业设计实战之线上水果超市商城的实现
2021-09-15 19:23:01
![](https://img.aspxhome.com/file/2023/9/62779_0s.png)
Mybatis实现关联关系映射的方法示例
2022-09-18 05:36:12
![](https://img.aspxhome.com/file/2023/9/128909_0s.jpg)
c# 动态加载dll文件,并实现调用其中的方法(推荐)
2021-11-10 04:37:34
Android中让按钮拥有返回键功能的方法及重写返回键功能
2021-08-08 10:33:56
maven profile实现多环境配置的示例
2023-09-24 12:02:57
![](https://img.aspxhome.com/file/2023/3/64513_0s.png)
Java7之forkjoin简介_动力节点Java学院整理
2023-08-31 14:08:28
![](https://img.aspxhome.com/file/2023/1/58341_0s.png)
Android中SparseArray性能优化的使用方法
2023-08-24 22:26:20
![](https://img.aspxhome.com/file/2023/8/139318_0s.jpg)
SpringBoot中@ConditionalOnBean实现原理解读
2023-04-25 14:24:54
![](https://img.aspxhome.com/file/2023/2/82432_0s.png)
Android仿百度图片查看功能
2023-09-26 07:50:24
![](https://img.aspxhome.com/file/2023/8/85448_0s.jpg)
谈谈你可能并不了解的java枚举
2023-11-09 21:08:55
![](https://img.aspxhome.com/file/2023/1/59021_0s.png)
phonegap教程使用jspdf库在应用中生成pdf文件(pdf生成方法)
2022-10-29 05:50:11
Java在指定路径上创建文件提示不存在解决方法
2021-07-31 10:06:21
Spring5路径匹配器PathPattern解析
2021-07-09 00:12:28
java操作solr实现查询功能的实例
2023-08-04 10:41:34
![](https://img.aspxhome.com/file/2023/0/95940_0s.png)
Android实现音量调节的方法
2023-01-01 20:23:45
![](https://img.aspxhome.com/file/2023/9/123299_0s.png)
Java框架之Maven SSM集合
2022-04-05 16:22:54
![](https://img.aspxhome.com/file/2023/8/77988_0s.png)
二叉树的遍历算法(详细示例分析)
2022-08-04 02:38:35
java.text.DecimalFormat用法详解
2022-09-30 03:02:27
![](https://img.aspxhome.com/file/2023/9/61249_0s.jpg)
C#实现窗体抖动的两种方法
2021-10-06 10:20:52
![](https://img.aspxhome.com/file/2023/6/80056_0s.jpg)