簡體   English   中英

定時加載另一項活動需要多長時間

[英]Timer how long it takes to load another activity

我想和你一樣。 您知道我如何計時加載另一項活動需要多長時間

例:

活動A

    onCreate....
        Intent myIntent = new Intent(this, ActivityB.class);
        finish();
        //START THE TIMER HERE **************
        startActivity(myIntent);
...

活動B

onCrate.....

loadPlayer();
....


private void loadPlayer() {

//Player has been loaded
//STOP THE TIME AND PRINT TO LOG CAT ***************
log.i("Timer", "It taken = ");

}

我創建了一個小型助手類,可以通過幾個簡單的應用程序完美滿足我的需求:

public class Profiler {

    private static HashMap<String, Long> profileTimes;

    public static void startProfiling(String key) {
        profileTimes.put(key, System.currentTimeMillis());
    }

    public static void endProfiling(String key) {
        endProfiling(key, "");
    }

    public static void endProfiling(String key, String desc) {
        if (profileTimes.get(key) != null) {
            long time = System.currentTimeMillis() - profileTimes.get(key);
            Log.d("profiling", key + ", " + desc + ": time: " + (time / 1000) + "." + String.format("%03d", (time % 1000)));
            profileTimes.remove(key);
        } else {
            Log.e("profiling", "NO profiling found for key: " + key);
        }
    }
}

要使用它,只需執行Profiler.startProfiling("ActivityB")並在考慮加載它時-> Profiler.endProfiling("ActivityB")

您必須采用兩個全局變量。

假設...

public static long TIME1, TIME2;

活動A

onCreate....
        Intent myIntent = new Intent(this, ActivityB.class);
        finish();
        //START THE TIMER HERE **************

        TIME1 = System.currentTimeMillis();

        startActivity(myIntent);
...

活動B

private void loadPlayer() {

    //Player has been loaded
    //STOP THE TIME AND PRINT TO LOG CAT ***************

    TIME2 = System.currentTimeMillis();

    log.i("Timer", "It taken = " + (TIME2 - TIME1));


    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM