[英]Timer how long it takes to load another activity
I wanted to as you. 我想和你一样。 Do you know how I can time how long it take to load another activity
您知道我如何计时加载另一项活动需要多长时间
example: 例:
Activity A 活动A
onCreate....
Intent myIntent = new Intent(this, ActivityB.class);
finish();
//START THE TIMER HERE **************
startActivity(myIntent);
...
Activity B 活动B
onCrate.....
loadPlayer();
....
private void loadPlayer() {
//Player has been loaded
//STOP THE TIME AND PRINT TO LOG CAT ***************
log.i("Timer", "It taken = ");
}
I created a small helper class that works perfect for my needs in a couple of simple apps: 我创建了一个小型助手类,可以通过几个简单的应用程序完美满足我的需求:
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);
}
}
}
To use it just do Profiler.startProfiling("ActivityB")
and when you consider it's loaded -> Profiler.endProfiling("ActivityB")
要使用它,只需执行
Profiler.startProfiling("ActivityB")
并在考虑加载它时-> Profiler.endProfiling("ActivityB")
You have to take two Global variables. 您必须采用两个全局变量。
Suppose... 假设...
public static long TIME1, TIME2;
Activity A 活动A
onCreate....
Intent myIntent = new Intent(this, ActivityB.class);
finish();
//START THE TIMER HERE **************
TIME1 = System.currentTimeMillis();
startActivity(myIntent);
...
Activity B 活动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.