开发者

Android application Internal timer

How do I keep track of the 开发者_如何学运维runtime of an application.


What do you mean by runtime? The time elapsed after you launch an application?

I don't think there is any direct method that can give you the elapsed time. You can maintain a timer on you own.

You can also use System.nanoTime() to get the most accurate timing.

long startTime = System.nanoTime();    
// ... the code being measured ...    
long estimatedTime = System.nanoTime() - startTime;

Edit---

Yes i think Ben is correct.. If you want to just measure how long a process is running then you can try using this approach android.os.Process.getElapsedCpuTime() - This should return the elapsed milliseconds your process has run. I believe your application also should run on a unique process ID. So that time should be fine


You could try to get the current time when you first start the app, and then when you want to know how long the app has run for just get the current time again and then subtract the two to get the difference.

// store this as a global variable when you start the app
long start = System.currentTimeMillis();

...

// run this method when you want to query how long the app has run for
public long getTimeInMillisecondsAppHasRun()
{
    long current = System.currentTimeMillis();
    return current - start;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜