接受的答案并没有具体回答这个问题。 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