“I / ActivityManager:显示......活动...... + 850ms”由什么组成?

时间:2015-09-29 12:53:57

标签: android android-logcat activity-manager

我正在尝试改善应用活动的显示时间。
我正在使用logcat来跟踪我的活动显示时间。

以下是logcat输出的示例:

I / ActivityManager(1097):显示com.example.myapp / com.example.myapp.activity.TutorialActivity:+ 850ms(总计+ 1s503ms)

有人能告诉我活动经理如何得出结论这是显示活动的时间吗?
在这段时间内会发生什么以及这段时间会考虑什么?
“正常时间”和“总时间”之间有什么区别?

我试图找到有关此事的材料,但没有成功。

提前谢谢!

1 个答案:

答案 0 :(得分:9)

此行打印在com.android.server.am.ActivityRecord.reportLaunchTimeLocked

private void reportLaunchTimeLocked(final long curTime) {
    final ActivityStack stack = task.stack;
    final long thisTime = curTime - displayStartTime;
    final long totalTime = stack.mLaunchStartTime != 0
            ? (curTime - stack.mLaunchStartTime) : thisTime;
    if (ActivityManagerService.SHOW_ACTIVITY_START_TIME) {
        Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
        EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME,
                userId, System.identityHashCode(this), shortComponentName,
                thisTime, totalTime);
        StringBuilder sb = service.mStringBuilder;
        sb.setLength(0);
        sb.append("Displayed ");
        sb.append(shortComponentName);
        sb.append(": ");
        TimeUtils.formatDuration(thisTime, sb);
        if (thisTime != totalTime) {
            sb.append(" (total ");
            TimeUtils.formatDuration(totalTime, sb);
            sb.append(")");
        }
        Log.i(ActivityManagerService.TAG, sb.toString());
    }
    mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
    if (totalTime > 0) {
        //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
    }
    displayStartTime = 0;
    stack.mLaunchStartTime = 0;
}

“正常时间”基本上是在即将启动Activity之间花费的时间,并且绘制了Activity的内容视图(包括绘图时间)。

“总时间”包括“正常时间”,并且还考虑了启动之前Activity所花费的时间。

通常,“正常时间”与“总时间”相同。您可以从源代码中看到

if (thisTime != totalTime) {
       sb.append(" (total ");
       TimeUtils.formatDuration(totalTime, sb);
       sb.append(")");
 }

只有在与“正常时间”不同时才会打印“总时间”。通常,如果活动B在活动A的onCreate中启动,活动B的“正常时间”将与“总时间”不同。