だから私は、sqlite データベースから情報を取得するソータ タイマー ウィジェットを作成しています。データベースにエントリが 1 つしかない場合は期待どおりに動作するように見えますが、複数ある場合、値の間でウィジェットがちらつくようです。たとえば、1 秒で最初の値が表示され、次の 2 秒で次の値が表示されます。コードは次のとおりです。リマインダー値を調べるコマンド fetchDecks(5) に注意してください。現在の時刻を過ぎている場合は表示されません。したがって、エントリ 1 の時間が終了すると、エントリ 2 に移動する必要があります。
public class MyWidgetProvider extends AppWidgetProvider {
private DBadapter mDbHelper;
Cursor note;
private long reminder;
private String title, summary;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
mDbHelper = new DBadapter(context);
mDbHelper.open();
note = mDbHelper.fetchDecks(5);
note.moveToFirst();
reminder = note.getLong(
note.getColumnIndexOrThrow(mDbHelper.KEY_REMINDER));
title = note.getString(
note.getColumnIndexOrThrow(mDbHelper.KEY_TITLE));
summary = note.getString(
note.getColumnIndexOrThrow(mDbHelper.KEY_SUMMARY));
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
}
private class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
thisWidget = new ComponentName(context, MyWidgetProvider.class);
}
@Override
public void run() {
long current = System.currentTimeMillis();
long diff = reminder - current;
remoteViews.setTextViewText(R.id.TextView01, countDown(diff));
remoteViews.setTextViewText(R.id.update, title);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
private String countDown(long result) {
String mMinutes,mSeconds,mHours, mDays;
int seconds = (int) (result / 1000) % 60 ;
if (seconds == 0) {
mSeconds = "0" + Integer.toString(seconds);
}
else if (seconds <= 9) {
mSeconds = "0" + Integer.toString(seconds);
} else {
mSeconds = Integer.toString(seconds);
}
int minutes = (int) ((result / (1000*60)) % 60);
if (minutes == 0) {
mMinutes = "0" + Integer.toString(minutes);
}
else if (minutes <= 9) {
mMinutes = "0" + Integer.toString(minutes);
} else {
mMinutes = Integer.toString(minutes);
}
int hours = (int) ((result / (1000*60*60)) % 24);
if (hours == 0) {
mHours = "0" + Integer.toString(hours);
}
else if (hours <= 9) {
mHours = "0" + Integer.toString(hours);
} else {
mHours = Integer.toString(hours);
}
int days = (int) (result / (1000*60*60*24));
if (days == 0) {
mDays = "0" + Integer.toString(days);
}
else if (days <= 9) {
mDays = "0" + Integer.toString(days);
} else {
mDays = Integer.toString(days);
}
String tRemaining = mDays + " days | " + mHours + " hours | " + mMinutes + " mins | " + mSeconds + " secs";
return tRemaining;
}
}
}