Android widget will not update

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(); } 
+4
source share
1 answer

You must remember that in one application there can be many widgets. The user can place two or more on the screen.

What you missed is a loop through identifiers. You passed some identifier of the component that you are generating, not the widget identifier, and therefore it does not update the correct widget.

Widgets can update at least 30 minutes. Every 1 second will not work if you are not using AlarmManager . You do not need it in your case.

 @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for (int appWidgetId : appWidgetIds) { 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(appWidgetId, remoteViews); } } 
+2
source

Source: https://habr.com/ru/post/1434014/


All Articles