t.oneill
t.oneill

Reputation: 37

When displaying SystemClock.elapsedRealtime into a text view via a button i am getting a very large value

When trying to display the elapsed time of my program, after clicking the button im receiving a very high value "119660898". If i wait for example 3 seconds and reclick the button the total will go up roughly 3 seconds as expected.

Can anyone explain why the initial value is so high? Below is the code i use in the button.

        FinalTime = SystemClock.elapsedRealtime();

        TextView text = (TextView) findViewById(R.id.textView1);
        text.setText("n " + FinalTime);

Upvotes: 0

Views: 345

Answers (1)

BackSlash
BackSlash

Reputation: 22243

When trying to display the elapsed time of my program, after clicking the button im receiving a very high value [...] Can anyone explain why the initial value is so high?

Because it is not the elapsed time of your program. From the docs:

elapsedRealtime() and elapsedRealtimeNanos() return the time since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for general purpose interval timing.

If you want to know the elapsed time since your app started, you need to save SystemClock.elapsedRealtime() when the app starts and calculate a delta afterwards.

Upvotes: 2

Related Questions