定位耗时方法
logcat查看
logcat中在启动应用的时候有Displayed 可以查看启动时间
01-08 14:04:44.619: I/ActivityManager(1459): Displayed org.codeaurora.snapcam/com.android.camera.CameraLauncher: +1s56ms
直接使用adb查看
adb shell am start -W com.example.myapplication/com.example.Dialog.DialogActivity
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.myapplication/com.example.Dialog.DialogActivity }
Status: ok
Activity: com.example.myapplication/com.example.Dialog.DialogActivity
ThisTime: 1513 最后一个activity启动的时间
TotalTime: 1513 一系列activity启动的时间
WaitTime: 1768 总启动时间,包括加载app信息到内存的时间
Complete
Debug.startMethodTracing
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File file = new File(Environment.getExternalStorageDirectory(), "app.trace");
Debug.startMethodTracing(file.getAbsolutePath());
init();
test();
Debug.stopMethodTracing();
}
private void test() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void init() {
test1();
}
private void test1() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
test2();
test();
}
private void test2() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
启动这个activity之后,把app.trace拉出来,然后直接把文件拖到AndroidStudio中查看
image.pngadb pull /storage/emulated/0/app.trace
image.png
网友评论