使用timertask每X秒更新一次时钟?

时间:2013-01-05 07:23:52

标签: java android

我正在尝试使用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);
        }
    }

我怎样才能让它发挥作用?

3 个答案:

答案 0 :(得分:1)

使用runOnUiThread从定时器线程

更新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);
}

答案 1 :(得分:0)

OR,定期将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();
    }
}

答案 2 :(得分: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;
    }
}