当我的应用程序启动时,我用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
期过后添加它。我该如何实现呢?
答案 0 :(得分:0)
我已经解决了这样的事情:用SharedPreferences创建了两个方法,我在其中放置了现在执行的方法的标志:
private void setWidgetActive(Context context, boolean active,String field){
Context appContext = context.getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(field, active);
edit.commit();
}
private boolean getWidgetActive(Context context, String field)
{
Context appContext = context.getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
return prefs.getBoolean(field, false);
}
然后,我的onEnabled方法:
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
setWidgetActive(context,true, WIDGET_ACTIVE);
}
在onUpdate方法中:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
setWidgetActive(context,true, WIDGET_UPDATE);
if (getWidgetActive(context, WIDGET_UPDATE) & !getWidgetActive(context, WIDGET_ACTIVE))
{
myString = context.getResources().getStringArray(R.array.quotes);
q = myString[rgenerator.nextInt(myString.length)];
mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
saveQuote(context, q, mydate);
}
else
{
ArrayList<Quote> importList = getQuotes(context);
q = importList.get(importList.size() - 1).getText();
mydate = importList.get(importList.size() - 1).getDate();
setWidgetActive(context,false, WIDGET_ACTIVE);
}
}
所以如果刚刚添加了widget,getWidgetActive(context,WIDGET_ACTIVE)返回true,我得到最后添加的数据库字段并将其发送到widget的TextView。在这种情况下,当定义的时间过去后,我从数组中获取新的随机stting并保存在数据库中并发送到TextView。