Show milliseconds with Android Chronometer

一世执手 提交于 2019-11-30 17:43:30

Is it possible to do this?

Not really. You could pass a format string that shows tenths of a second, but the Chronometer itself only updates every second. The update frequency is baked into the code.

If not, is there a free (and preferably open source) library that does the same?

The Chronometer source is available under the Apache License 2.0, so you can modify it to suit. Find all occurrences of 1000 and change them to 100, and you're probably most of the way there.

Bear in mind that this Chronometer can be used in an activity but not an app widget, since customized View classes are not supported by the app widget framework.

Android's default chronometer widget does not support millisecond formatting.

I have modified the Chronometer widget source code in order to show milliseconds. Download it from github.

Bharat Ahuja

I created an instance of a class that I built inside the Activity. This new class was an extension of the Android CountDownTimer class(add the implemented methods)

Here in the constructor you can add the duration in milliseconds and the interval to be 1/10 of a second, that is 100 milliseconds. In the onTick method, the commands are executed every 1/100th of a second for the given duration. That should work.

I have found the way after lots and lots of research. I don't know if it's effective or not, because the truth is that the Emulator crashes after about 10 seconds, but on the phone it runs just fine, and that does it for me. It looks something like this:

import android.os.Handler;
public class Chronometer extends Activity {
  private Handler mHandler = new Handler();
public void actions() {
  mHandler.removeCallbacks(mUpdateTimeTask);
  mHandler.postDelayed(mUpdateTimeTask, 1); //1 is the number of milliseconds you want the text to be updated
}
Runnable mUpdateTimeTask = new Runnable() {
  public void run() {
    i++;
    txt.setText(formatTime(i)); // formatTime is just for make it readable in HH:MM:SS:MS, but I assume you already have this
    mHandler.postDelayed(this, 1);
    }
  };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!