I have a widget with two TextView showing the time and date.
I simply want this batch of code to happen every second:
views.setOnClickPendingIntent(R.id.rl, pendingIntent);
java.util.Date noteTS = Calendar.getInstance().getTime();
String time = "kk:mm";
String date = "dd MMMMM yyyy";
views.setTextViewText(R.id.tvTime, DateFormat.format(time, noteTS));
views.setTextViewText(R.id.tvDate, DateFormat.format(date, noteTS));
So I need a simple way to update TextView periodically in the widget so the time changes more than the standard 30 mins. Thanks
Another possibility would be the use of the Timer and TimerTask classes. You can use the
Timer.schedule(TimerTask task, long delay, long period)
method to accomplish what you need.Keep in mind that you cannot access the UI thread from within a TimerTask - so the
Handler.postDelayed()
method might be a better bet if you already intend to use aHandler
with theTimer
class. There are other methods for accessing the UI thread with aTimerTask
, such asActivity.runOnUiThread()
.you may create
Handler
andRunnable
, and then let theHandler
to restart yourRunnable
at the specified interval. your program might look like this:and inside
updateClock()
you do all your UI updates. I suppose all this happens inside theActivity
with the appropriateonPause()/onResume()
calls.u have to use AlarmManager, check this tutorial for details: http://www.vogella.com/articles/AndroidWidgets/article.html#widget_update dont forget to check this part:
8. Tutorial: Update widget via a service