当我的应用程序启动时,我使用 listview 和第一个项目调用活动。我的小部件在 onUpdate() 中将新的一项添加到 listView:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main_widget);
myString = context.getResources().getStringArray(R.array.quotes);
Intent active = new Intent(context, Widget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
Intent archive = new Intent(context, Widget.class);
archive.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
archive.setAction(ACTION_WIDGET_ARCHIVE);
String q = myString[rgenerator.nextInt(myString.length)];
active.putExtra("msg", q);
if (getQuotes(context).size()>1)
remoteViews.setTextViewText(R.id.widget_textview, q);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
PendingIntent archivePendingIntent = PendingIntent.getBroadcast(context, 0, archive,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetFrameLayout, archivePendingIntent);
Time now = new Time();
now.setToNow();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
// adding new item to the listView
saveQuote(context, q, mydate);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
并且当用户将小部件添加到屏幕时,会自动添加一个项目到列表中。但是我需要在android:updatePeriodMillis
期限过去后添加它。我该如何实施?