`handler.postDelayed`无法正常工作

huangapple 未分类评论46阅读模式
英文:

handler.postDelayed not working correctly

问题

我有一个简单的秒表代码片段。线程在自定义类中运行,它通过接口连接到主活动

  1. public class MainActivity extends AppCompatActivity implements MainActivityInteractionInterface{
  2. public static boolean isRunning = false;
  3. Stopwatch stopWatch;
  4. private TextView textViewMilliSeconds;
  5. private TextView textViewSeconds;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. textViewMilliSeconds = findViewById(R.id.textViewStopwatchMilliseconds);
  11. textViewSeconds = findViewById(R.id.textViewStopwatchSeconds);
  12. stopWatch = new Stopwatch(this, getApplicationContext());
  13. stopWatch.runThread();
  14. }
  15. @Override
  16. public void updateUI() {
  17. String time = String.format(Locale.getDefault(), "%03d", stopWatch.getMilliseconds());
  18. textViewMilliSeconds.setText(time);
  19. String timeSeconds = String.format(Locale.getDefault(), "%02d", stopWatch.getSeconds());
  20. textViewSeconds.setText(timeSeconds);
  21. }
  22. public void startTimer(View view) {
  23. isRunning = !isRunning;
  24. }
  25. }
  1. public class Stopwatch {
  2. private int milliseconds = 0;
  3. private int seconds = 0;
  4. public int getMilliseconds() {
  5. return milliseconds;
  6. }
  7. public int getSeconds() {
  8. return seconds;
  9. }
  10. private MainActivityInteractionInterface interactionInterface;
  11. private Context applicationContext;
  12. public Stopwatch(MainActivityInteractionInterface interactionInterface, Context applicationContext){
  13. this.interactionInterface = interactionInterface;
  14. this.applicationContext = applicationContext;
  15. }
  16. public void runThread(){
  17. final Handler handler = new Handler();
  18. handler.post(new Runnable(){
  19. @Override
  20. public void run(){
  21. if(isRunning) {
  22. milliseconds++;
  23. if (milliseconds == 1000) {
  24. milliseconds = 0;
  25. seconds++;
  26. if(seconds == 60){
  27. seconds = 0;
  28. }
  29. }
  30. }
  31. interactionInterface.updateUI();
  32. handler.postDelayed(this, 1);
  33. }
  34. });
  35. }
  36. }

处理程序应该每1毫秒更新一次,在达到1000毫秒时,经过1秒。如果我设置handler.postDelayed延迟低于15,达到1000毫秒将需要确切的18秒,为什么?

英文:

I have a simple stopwatch code piece. Thread is running in custom class, it connects to the main activity via Interface

  1. public class MainActivity extends AppCompatActivity implements MainActivityInteractionInterface{
  2. public static boolean isRunning = false;
  3. Stopwatch stopWatch;
  4. private TextView textViewMilliSeconds;
  5. private TextView textViewSeconds;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. textViewMilliSeconds = findViewById(R.id.textViewStopwatchMilliseconds);
  11. textViewSeconds = findViewById(R.id.textViewStopwatchSeconds);
  12. stopWatch = new Stopwatch(this, getApplicationContext());
  13. stopWatch.runThread();
  14. }
  15. @Override
  16. public void updateUI() {
  17. String time = String.format(Locale.getDefault(), "%03d", stopWatch.getMilliseconds());
  18. textViewMilliSeconds.setText(time);
  19. String timeSeconds = String.format(Locale.getDefault(), "%02d", stopWatch.getSeconds());
  20. textViewSeconds.setText(timeSeconds);
  21. }
  22. public void startTimer(View view) {
  23. isRunning = !isRunning;
  24. }

  1. public class Stopwatch {
  2. private int milliseconds = 0;
  3. private int seconds = 0;
  4. public int getMilliseconds() {
  5. return milliseconds;
  6. }
  7. public int getSeconds() {
  8. return seconds;
  9. }
  10. private MainActivityInteractionInterface interactionInterface;
  11. private Context applicationContext;
  12. public Stopwatch(MainActivityInteractionInterface interactionInterface, Context applicationContext){
  13. this.interactionInterface = interactionInterface;
  14. this.applicationContext = applicationContext;
  15. }
  16. public void runThread(){
  17. final Handler handler = new Handler();
  18. handler.post(new Runnable(){
  19. @Override
  20. public void run(){
  21. if(isRunning) {
  22. milliseconds++;
  23. if (milliseconds == 1000) {
  24. milliseconds = 0;
  25. seconds++;
  26. if(seconds == 60){
  27. seconds = 0;
  28. }
  29. }
  30. }
  31. interactionInterface.updateUI();
  32. handler.postDelayed(this, 1);
  33. }
  34. });
  35. }

handler should update every 1 millisec, when there is 1000 milliseconds, 1 second passes by
If I set handler.postDelayed delay anything below 15 reaching 1000 milliseconds would take exactly 18 seconds, why?

答案1

得分: 1

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

依我拙见,将其更新间隔设置为20毫秒,并相应地更改计数器的值,如下所示:

  1. handler.post(new Runnable(){
  2. @Override
  3. public void run(){
  4. if(isRunning) {
  5. milliseconds++;
  6. if (milliseconds == 50) {
  7. milliseconds = 0;
  8. seconds++;
  9. if(seconds == 60){
  10. seconds = 0;
  11. }
  12. }
  13. }
  14. interactionInterface.updateUI();
  15. handler.postDelayed(this, 20);
  16. }
  17. });
英文:

I don't know why it would take up to 18seconds, but I can tell you this: Android refresh the UI every 16msec (to have a rate of 60fps), so setting the handler to updateUI in a lesser time would make no sense and maybe also interfier with it.

In my humble opinion, make it to update in 20msec and change the counter values according, like this:

  1. handler.post(new Runnable(){
  2. @Override
  3. public void run(){
  4. if(isRunning) {
  5. milliseconds++;
  6. if (milliseconds == 50) {
  7. milliseconds = 0;
  8. seconds++;
  9. if(seconds == 60){
  10. seconds = 0;
  11. }
  12. }
  13. }
  14. interactionInterface.updateUI();
  15. handler.postDelayed(this, 20);
  16. }
  17. });

答案2

得分: 0

看看 handler.postDelayed(this, 1); 的第二个参数。

根据你增加毫秒的方式进行修改。

英文:

Look at the second argument of handler.postDelayed(this, 1);

Change it according to the way you increment your milliseconds.

huangapple
  • 本文由 发表于 2020年4月5日 20:10:47
  • 转载请务必保留本文链接:https://java.coder-hub.com/61042358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定