Android: How do I display an updating clock in a TextView

前端 未结 6 981
时光说笑
时光说笑 2020-12-08 02:49

have a clock I want to display in a TextView.

I would have thought there was a few nice functions to do this, but I couldn\'t find anything. I ended up implementing

6条回答
  •  广开言路
    2020-12-08 03:30

    Updating the system clock is not like updating a chronometer. We talk about the time changed on the minute (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.

提交回复
热议问题