接受的答案並沒有具體回答這個問題。 OP要求一種方法來接收某種事件準時(在系統時鐘分00秒)。
使用計時器不是正確的方法。這不僅是矯枉過正,但你必須採取一些技巧,使其正確。
做到這一點(即更新顯示的時間爲HH一個TextView:毫米),正確的方法是使用BroadcastReceiver的是這樣的:
BroadcastReceiver _broadcastReceiver;
private final SimpleDateFormat _sdfWatchTime = new SimpleDateFormat("HH:mm");
private TextView _tvTime;
@Override
public void onStart() {
super.onStart();
_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
_tvTime.setText(_sdfWatchTime.format(new Date()));
}
};
registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}
@Override
public void onStop() {
super.onStop();
if (_broadcastReceiver != null)
unregisterReceiver(_broadcastReceiver);
}
該系統將在確切的開始發送該廣播事件每分鐘基於系統時鐘。不要忘記,事先初始化您的TextView(到當前系統時間),因爲它很可能會在一分鐘之內彈出您的用戶界面,並且在下一分鐘發生之前TextView不會更新。
三江源,這種想法的工作。我得到了當前的日期,減去秒和毫秒,所以這是一個四捨五入的分鐘。然後,我加了一整分鐘,以便開始。最後,我把它設置爲每分鐘更新一次(60 000毫秒)的定時器。 – Mel 2011-03-31 00:00:25
如果有人感興趣,下面是一些代碼。 Steve Ody編寫的代碼(http://steve.odyfamily.com/?p=12)非常有用。 'code'myTimer = new Timer(); GregorianCalendar calCreationDate = new GregorianCalendar(); int intMilli = calCreationDate.get(Calendar.MILLISECOND); int intSeconds = calCreationDate.get(Calendar.SECOND); calCreationDate.add(Calendar.MILLISECOND,(-1 * intMilli)); calCreationDate.add(Calendar.SECOND,-1 * intSeconds); calCreationDate.add(Calendar.MINUTE,1);日期dateStartDate = calCreationDate.getTime(); 'code' – Mel 2011-03-31 00:03:15
Android工作中的計時器,但它們不是首選的方式。使用處理程序而不是定時器! – Zordid 2013-06-04 07:58:11