2015-04-01 329 views
0

我想和你一樣。你知道我怎麼可以時間需要多長時間來加載另一個活動計時器加載其他活動需要多長時間

例如:

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

} 
+0

製作靜態參照計時器或替代的起始前的計時器你開始另一個活動在您調用活動的oncreate方法中的'loadPlayer'方法之前,您調用了它,而不是在調用 – 2015-04-01 02:45:00

+0

的成員變量時,例如私有長mTimer; 開始計時器:mTimer = System.currentTimeMillis(); 停止計時器:log.i(「Timer」,System.currentTimeMillis() - mTimer); 這就是毫秒btw。 – 2015-04-02 08:18:13

回答

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"),當你考慮它的加載 - >Profiler.endProfiling("ActivityB")

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


    }