0

我正在尝试使用 timertask 更新我的数字时钟。我创建了一个名为 updateClock() 的函数,它将小时和分钟设置为当前时间,但我无法让它定期运行。从我在其他答案中读到的内容来看,最好的选择之一是使用 timertask 但是我无法制作任何我在 Android 活动中发现在线工作的示例。

这是我到目前为止所写的:

public class MainActivity extends Activity {
    TextView hours;
    TextView minutes;
    Calendar c;
    int cur_hours;
    int cur_minutes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clock_home);
        hours = (TextView) findViewById(R.id.hours);
        minutes = (TextView) findViewById(R.id.minutes);
        updateClock();
        }

    public void updateClock() {
        c = Calendar.getInstance();
        hours.setText("" + c.get(Calendar.HOUR));
        minutes.setText("" + c.get(Calendar.MINUTE));
        }

    public static void init() throws Exception {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                    updateClock(); // ERROR
                }
            }, 0, 1 * 5000);
        }
    }

我怎样才能让它工作?

4

3 回答 3

1

使用runOnUiThread从 Timer Thread 更新 Ui

timer.scheduleAtFixedRate(new TimerTask() {
     public void run() {
      MainActivity.this.runOnUiThread (new Runnable() { 
         public void run() {
             updateClock(); // call UI update method here
         }
     }));  
   }
 }, 0, 1 * 5000);
}
于 2013-01-05T07:26:48.850 回答
0

或者,定期将 Runnable 发布到 UI 线程的 Handler。此外,暂停和恢复任务以节省电池。

public class MyActivity extends Activity {
    private final Handler mHandler = new Handler();
    private final Timer mTimer = new Timer();

    @Override
    protected void onResume() {
        super.onResume();

        mTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        //---update UI---
                    }
                });
            }
        },0,5000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mTimer.cancel();
    }
}
于 2013-01-05T07:35:56.437 回答
0

如果您只需要每分钟更新一次,您还可以收听ACTION_TIME_TICK广播事件。

private boolean timeReceiverAttached;
private final BroadcastReceiver timeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateClock();
    }
};
private Handler handler = new Handler();

@Override
protected void onResume() {
    super.onResume();
    updateClock();
    if (!timeReceiverAttached) {
        timeReceiverAttached = true;
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        registerReceiver(timeReceiver, filter, null, handler);
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (timeReceiverAttached) {
        unregisterReceiver(timeReceiver);
        timeReceiverAttached = false;
    }
}
于 2013-01-05T07:37:52.517 回答