The system sends a broadcast event at the exact start of each minute based on the system clock. Create a service with your widgets and do something 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); }
Do not forget, however, to initialize your TextView in advance (before the current system time), since most likely you will pull out your interface in the middle of a minute, and the TextView will not be updated until the next minute.
Alex
source share