I am working on widgets for Android. One of the things he has to do is display the current day of the week and day of the month. I think my code is fine, but for some reason it is never updated. My provider has an update period of 30 minutes, but I donβt think it should matter (in any case, I tried to set it to 1 second and it didnβt change anything). Also, if I force it to print values ββfor the current day of the week and day of the month in LogCat, it works fine, so I really don't know why it is not being updated. Please help me! This is my code:
public class Henk extends AppWidgetProvider { AppWidgetManager appWidgetManager; ComponentName componentName; RemoteViews remoteViews; LocationManager locationManager; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Update the current date this.appWidgetManager = appWidgetManager; componentName = new ComponentName(context, Henk.class); remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); SimpleDateFormat dayofweekformat = new SimpleDateFormat("EEEE"); Date dayofweekdate = new Date(System.currentTimeMillis()); String dayofweeklowercase = dayofweekformat.format(dayofweekdate); String dayofweek = dayofweeklowercase.toUpperCase(); SimpleDateFormat monthformat = new SimpleDateFormat("MMMM dd, yyyy"); Date monthdate = new Date(System.currentTimeMillis()); String month = monthformat.format(monthdate); Log.d("TAG", "----> DAY OF WEEK: " + dayofweek); // Fine in LogCat Log.d("TAG", "----> MONTH AND DATE: " + month); // Fine in LogCat remoteViews.setTextViewText(R.id.widget_textview1, dayofweek); remoteViews.setTextViewText(R.id.widget_textview2, month); appWidgetManager.updateAppWidget(componentName, remoteViews); } }
EDIT:
I implemented the solution provided by Doomsknight , so now I think my onUpdate () method should be fine. However, he still does not show me the day and date. I noticed, however, when I tested it, then onUpdate () actually executes before closing my configuration. In my configuration setup, I have the following code to initialize my widget (at least what it should do), and I think there is an error here:
public void onClick(View view) { // Launch the Widget and close the configuration Activity Intent intent2 = new Intent(context, Henk.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 0); remoteViews.setOnClickPendingIntent(R.id.configuration_button, pendingIntent); appWidgetManager.updateAppWidget(appWidgetID, remoteViews); Log.d("TAG", "----> APP WIDGET ID: " + appWidgetID); Log.d("TAG", "----> REMOTEVIEWS: " + remoteViews); Intent result = new Intent(); result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetID); setResult(RESULT_OK, result); finish(); }