Best way to update TextView, every minute, on the minute

与世无争的帅哥 提交于 2019-11-27 15:07:58

问题


My problem is similar to this one, How to update a widget every minute, however I only want to update TextView of the UI. I need access to the time so unfortunately can not simply use the DigitalClock view.

Ive been researching and have found ways to update every minute, but not on the minute so they are synchronised with the system clock (so they might be up to 59 seconds out of sync!)

The only ways I can think to do it are (a) Run a handler (or timer) every second (which seems a bit like overkill if I only want to update every minute) Like this from the android.com resources

(b) Have a service running in background. (I'm not keen on this as I already have more important things running in the background)

(c) use Alarm Manager (again seems like overkill)

Seems to be such an easy thing to do, and yet...

Any advice appreciated Mel


回答1:


You can try configuring a Timer to run a TimerTask that updates your TextView every minute (see its schedule method).




回答2:


The accepted answer does not respond specifically to the question. The OP asks for a way to receive some sort of event on time (at the system clock minute and 00 seconds).

Using a Timer is not the right way to do this. It's not only overkill, but you must resort to some tricks to make it right.

The right way to do this (ie. update a TextView showing the time as HH:mm) is to use BroadcastReceiver like this :

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

The system will send this broadcast event at the exact beginning of every minutes based on system clock. Don't forget however to initialize your TextView beforehand (to current system time) since it is likely you will pop your UI in the middle of a minute and the TextView won't be updated until the next minute happens.




回答3:


have you tried postDelayed()




回答4:


This is what I used :

int secsUntilOnTheMinute=60-Calendar.getInstance().get(Calendar.SECOND);

timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        updateSendTimesHandler.sendEmptyMessage(0);
    }
}, secsUntilOnTheMinute*1000, 60000);


来源:https://stackoverflow.com/questions/5457186/best-way-to-update-textview-every-minute-on-the-minute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!