Search code examples
androidwidgetonupdate

Android Widget Won't Update


I have been working on a widget for Android. One of the things it should do is display the current day of the week and day in the month. I think my code is alright, but for some reason it never updates. The update period in my provider is set to 30 minutes, but I don't think that should matter (in any case I've tried setting it to 1 second and it didn't change anything). Also, if I make it print the values for the current day of the week and day in the month in LogCat it works fine, so then I really don't know why it isn't updating. Please help me out! 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 have implemented the solution provided by Doomsknight, so now I think my onUpdate() method should be alright. It still doesn't show me the day and date yet though. I noticed however, when I was test running it, that onUpdate() is actually executed before my configuration activity is closed. In my configuration activity I have the following code to initialize my widget (at least that's what it should do), and I think the error is in here then:

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();
}

Solution

  • You have to bear in mind that there can be many widgets to one application. The user can place two or more onto your screen.

    What you have missed is the looping through the Ids. You have passed in some component ID that you generate, rather than the widgets ID and so it does not update the correct widget.

    Widgets can only update minimum 30 mins. Every 1 second will not work, unless you use an AlarmManager. You do not need this 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);
        }
    }