所以我正在制作一个从 sqlite 数据库中提取信息的排序计时器小部件。当数据库中只有 1 个条目时看起来按预期工作,但当有超过 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;
}
}
}