计时器加载另一个活动需要多长时间

时间:2015-04-01 02:40:17

标签: java android performance android-activity timer

我想和你一样。你知道如何计算加载另一项活动需要多长时间

示例:

活动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 = ");

}

2 个答案:

答案 0 :(得分:1)

我创建了一个小帮助程序类,可以在几个简单的应用程序中完美地满足我的需求:

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")并在考虑加载时使用&gt; Profiler.endProfiling("ActivityB")

答案 1 :(得分:1)

你必须采取两个全局变量。

...假设

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));


    }