When my application starts, I call activity with listview and first item to it. my widget adds new one item to the listView in the onUpdate():
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);
}
and when user adds widget to the screen, one more items adds to list automatically. But I need to add it after android:updatePeriodMillis
period has passed. How can I implement this?
I've solved it something like this: created two methods with SharedPreferences, where I put flags of what method is executes now:
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);
}
then, my onEnabled method:
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
setWidgetActive(context,true, WIDGET_ACTIVE);
}
in onUpdate method:
@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);
}
}
so if widget was added just now, getWidgetActive(context, WIDGET_ACTIVE) returns true and I get last added field of database and send it to the TextView of the widget. And in that case, when defined time has passed, I get new random stting from array and save in database and also send to the TextView.