java - handler.postDelayed 无法正常工作

标签 java android android-handler

我有一个简单的秒表代码 fragment 。线程在自定义类中运行,它通过接口(interface)连接到主 Activity

public class MainActivity extends AppCompatActivity implements MainActivityInteractionInterface{

public static boolean isRunning = false;
Stopwatch stopWatch;
private TextView textViewMilliSeconds;
private TextView textViewSeconds;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textViewMilliSeconds = findViewById(R.id.textViewStopwatchMilliseconds);
    textViewSeconds = findViewById(R.id.textViewStopwatchSeconds);

    stopWatch = new Stopwatch(this, getApplicationContext());
    stopWatch.runThread();
}

@Override
public void updateUI() {
    String time = String.format(Locale.getDefault(), "%03d", stopWatch.getMilliseconds());
    textViewMilliSeconds.setText(time);

    String timeSeconds = String.format(Locale.getDefault(), "%02d", stopWatch.getSeconds());
    textViewSeconds.setText(timeSeconds);
}

public void startTimer(View view) {
    isRunning = !isRunning;
}
<小时/>
public class Stopwatch {
private int milliseconds = 0;
private int seconds = 0;

public int getMilliseconds() {
    return milliseconds;
}

public int getSeconds() {
    return seconds;
}

private MainActivityInteractionInterface interactionInterface;
private Context applicationContext;

public Stopwatch(MainActivityInteractionInterface interactionInterface, Context applicationContext){
    this.interactionInterface = interactionInterface;
    this.applicationContext = applicationContext;
}

public void runThread(){
    final Handler handler = new Handler();
    handler.post(new Runnable(){
        @Override
        public void run(){
            if(isRunning) {
                milliseconds++;
                if (milliseconds == 1000) {
                    milliseconds = 0;
                    seconds++;
                    if(seconds == 60){
                        seconds = 0;
                    }
                }
            }
            interactionInterface.updateUI();
            handler.postDelayed(this, 1);
        }
    });
}

handler应该每1毫秒更新一次,当有1000毫秒时,1秒就过去了 如果我将 handler.postDelayed 延迟设置为低于 15 达到 1000 毫秒,则需要 18 秒,为什么?

最佳答案

我不知道为什么需要长达 18 秒的时间,但我可以告诉你:Android 每 16 毫秒刷新一次 UI(速率为 60fps),因此将处理程序设置为 updateUI 在更短的时间内就没有任何意义,而且可能还会干扰它。

以我的拙见,使其在 20 毫秒内更新并根据情况更改计数器值,如下所示:

handler.post(new Runnable(){
    @Override
    public void run(){
        if(isRunning) {
            milliseconds++;
            if (milliseconds == 50) {
                milliseconds = 0;
                seconds++;
                if(seconds == 60){
                    seconds = 0;
                }
            }
        }
        interactionInterface.updateUI();
        handler.postDelayed(this, 20);
    }
});

关于java - handler.postDelayed 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61042358/

相关文章:

java - 我如何 'get' 帮助字符串来自 commons cli 而不是 'print'

java - "The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files"

java - 如何解决AWS Elastic Beanstalk中的SMTP连接超时异常?

java - Android 蓝牙发送/接收 HashMap

java - JMeter 从路径参数开头删除正斜杠

android - 缺少 Google Maps API V2 google-play-services_lib.jar

android - StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

android - 发生数据库更改后更新 UI 的方法

android - 处理程序延迟 60 秒不工作?

Android:创建一个定期运行并执行 UI 任务的后台线程?