Android - 记录启动延迟的问题

Sid*_*Sid 13 android adb android-4.4-kitkat nexus-5

我正在尝试记录我的应用程序的启动延迟.我这样做的方法是设置应用程序的开始时间Application.onCreate并提供返回时间的公共方法.

MyApplication extends Application {
    Date startUpTime;
    //Declare variables
    @Override
    public void onCreate() {
        super.onCreate();
        setStartupTime();
        //other initializations
    }

    private void setStartUpTime() {
        startUpTime = new Date();
    }

    public Date getStartUpTime() {
        return startUpTime;
    }
}

MyActivity extends Activity {
.
.
.
    @Override
    public void onStart(){
        logStartUpLatency();
        //other onStart stuff
    }

    private void logStartUpLatency() {
        Date currentTime = new Date();
        Date startTime = (MyApplication)getApplicationContext().getStartUpTime();
        long latency = currentTime.getTime() - startTIme.getTime();
        Log.d("Start up Latency is ", Long.toString(latency)):

    }
Run Code Online (Sandbox Code Playgroud)

这就是我测试启动延迟的方法:

  • adb安装myapk
  • 运行应用程序以获得第一次启动延迟.我可以看到记录的延迟对于第一次启动是正确的
  • 再次运行应用程序以测试启动延迟.记录的延迟对于开始(或任何后续启动次数)是正确的
  • 现在我将应用程序的版本代码和名称增加1.为了模拟升级,我使用了命令adb install -r myapk.
  • 现在我再次运行应用程序以测试升级后的第一个启动延迟,即使需要3秒,记录的延迟也不在图表中.

有人知道为什么会这样吗?

更新

因此,如果我使用"adb install -r myapk"安装apk,则应用程序不会通过Myapplication.onCreate().

Seb*_*ano 10

我建议使用这TimingLogger门课.根据文档,您可以轻松跟踪已用时间,甚至可以在流程中添加拆分.

这个

TimingLogger timings = new TimingLogger(TAG, "methodA");
// ... do some work A ...
timings.addSplit("work A");
// ... do some work B ...
timings.addSplit("work B");
// ... do some work C ...
timings.addSplit("work C");
timings.dumpToLog();
Run Code Online (Sandbox Code Playgroud)

产生

D/TAG (3459): methodA: begin
D/TAG (3459): methodA:      9 ms, work A
D/TAG (3459): methodA:      1 ms, work B
D/TAG (3459): methodA:      6 ms, work C
D/TAG (3459): methodA: end, 16 ms
Run Code Online (Sandbox Code Playgroud)


Sid*_*Sid 0

因此,如果我使用“adb install -r myapk”安装 apk,该应用程序不会通过 Myapplication.onCreate()。那么这就回答了这个问题。我将问一个单独的问题,为什么使用“adb install -r myapk”安装应用程序,然后启动 myapk 不通过 MyApplication.onCreate()