简单的方法来更新TextView的小部件定期方法来、部件、简单、TextView

2023-09-07 04:05:42 作者:惜nī如命

我有两个TextView的一个小部件显示时间和日期。

I have a widget with two TextView showing the time and date.

我只是希望这批code的发生每秒:

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

所以,我需要一个简单的方法,在小部件定期更新的TextView所以时间的变化比标准的30分钟以上。谢谢

So I need a simple way to update TextView periodically in the widget so the time changes more than the standard 30 mins. Thanks

推荐答案

您可以创建处理程序的Runnable ,然后让处理程序在指定的时间重新启动的Runnable 。你的程序可能是这样的:

you may create Handler and Runnable, and then let the Handler to restart your Runnable at the specified interval. your program might look like this:

private Handler mHandler = new Handler();

@Override
public void onResume() {
    super.onResume();
    mHandler.removeCallbacks(mUpdateClockTask);
    mHandler.postDelayed(mUpdateCLockTask, 100);
}

@Override
public void onPause() {
    super.onPause();
    mHandler.removeCallbacks(mUpdateClockTask);
}

private Runnable mUpdateClockTask = new Runnable() {
    public void run() {
        updateClock();
        mHandler.postDelayed(mUpdateClockTask, 1000);
    }
};

和里面的 updateClock()你做你的所有UI更新。我想这一切都发生在活动与适当的的onPause()/ onResume()通话中。

and inside updateClock() you do all your UI updates. I suppose all this happens inside the Activity with the appropriate onPause()/onResume() calls.