java - 计时器加载另一个 Activity 需要多长时间

标签 java android performance android-activity timer

我想像你一样。你知道我如何计算加载另一个 Activity 需要多长时间

示例:

Activity A

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

Activity 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")

关于java - 计时器加载另一个 Activity 需要多长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29381981/

相关文章:

java - 我在使用正则表达式模式时遇到问题

android - 在 android 中处理动态数据类型

android - 从android中的视频文件中提取帧

java - 计算错误

java - 向 jaeger 代理发送跟踪时出现问题

java - 日期的自然语言生成器 (Java)

java - 重复方法调用 VS 局部变量

javascript - jQuery .hover() 性能疑虑

java - Spring Hibernate 多对多关联与 transient 值

java - 使用字符串、变量、循环的正确编码实践 - Java (Android)