App性能优化-app启动时间消耗获取方式-tracing
我们平常做app到一定的量产阶段就要考虑到性能优化这块,尤其是用户的第一感官app启动时间优化,那么我们怎么知道一个application或者activity中某个方法消耗了多长的时间.tracing可以非常好的定位到.例如我自定义一个Application,然后在onCreate方法中定义两个耗时方法一个是500ms,一个是300ms,然后使用Debug.startMethodTracing(“launcher_tracing”) 和Debug.stopMethodTracing()包裹起来."launcher_tracing"这个表示生成tracing的路径.生成的路径是在sdcard/Android/packageName/file文件中.运行之后我们可以通过android studio的profiler工具导入查看.
Debug.startMethodTracing("launcher_tracing");
stepOne();
stepTwo();
Debug.stopMethodTracing();
private void stepTwo() {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void stepOne() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}