Inactive widgets for Android are not updating

I am trying to implement an android app with some home / lock screen widgets! At this point, the content of the widget is fine when it is created but not updated.

Here is my MyWidgetProvider code

public class MyWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                         int[] appWidgetIds) {

        Integer[] preferencesIds = MyPreferences.getWidgetIds(context);
        Log.d(MyWidgetProvider.class.getSimpleName(), "In SP there is " + Arrays.toString(preferencesIds));


        if (preferencesIds != null && preferencesIds.length > 0) {
            for (int appWidgetId : preferencesIds) {
                Log.i(MyWidgetProvider.class.getSimpleName(), "Update widget " + appWidgetId + " from preferences");
                updateWidgetId(context, appWidgetManager, appWidgetId);
            }
        }

        if (appWidgetIds != null && appWidgetIds.length > 0) {
            for (int appWidgetId : appWidgetIds) {
                Log.i(MyWidgetProvider.class.getSimpleName(), "Update widget " + appWidgetId + " from base");
                updateWidgetId(context, appWidgetManager, appWidgetId);
            }
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        Log.d(MyWidgetProvider.class.getSimpleName(), "Widget with id " + Arrays.toString(appWidgetIds) + "has been deleted");
        for (int i = 0; i < appWidgetIds.length; i++) {
            MyPreferences.removeWidgetId(context, appWidgetIds[i]);
        }
        super.onDeleted(context, appWidgetIds);
    }

    /**
     * Update a widget with a specific id
     *
     * @param context          app context
     * @param appWidgetManager the widget manager
     * @param appWidgetId      the widget id to update
     */
    private void updateWidgetId(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        MyPreferences.addWidgetId(context, appWidgetId);

        Log.d(MyWidgetProvider.class.getSimpleName(), "Widget with id " + appWidgetId + " has been updated");
        RemoteViews rv = new RemoteViews(context.getPackageName(),
                R.layout.widget);
        Intent intent = new Intent(context, StickyWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        rv.setRemoteAdapter(android.R.id.list, intent);
        // Handle click
        Intent activityIntent = new Intent(context, NotesListActivity.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, activityIntent, 0);
        rv.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
        // Return
        appWidgetManager.updateAppWidget(appWidgetId, rv);
    }
}

      

The idea is to keep all widget ids in general app preference, so I can update them (forcing an update with intent), for example in mine MainActivity

, I have something like

/**
 * Starts an intent that force the service to wake up and update
 * the widgets
 */
private void updateWidgets() {
    Intent intent = new Intent(this, MyWidgetProvider.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
    // since it seems the onUpdate() is only fired on that:
    int[] ids = {R.xml.widget_infos}; // As I store the ids, this is kind of useless ?
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    sendBroadcast(intent);
}

      

But the problem is that in logcat I got the right solution (3 widgets updated, it prints IDs) and only the last widget is updated on my screen.

If anyone has a clue or ... anything, that would be great because it drives me crazy: /

+3


source to share


1 answer


In action, you can program all the IDs of the app-related widgets installed on the user's home / lock screen.

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(application);
final int[] smallAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(application, SmallWidgetProvider.class));
for (int appWidgetId : smallAppWidgetIds) {
  appWidgetManager.updateAppWidget(appWidgetId, 
    SmallWidgetProvider.buildRemoteView(application, appWidgetId, whatEverYouNeed));
}

      

AppWidgetManager.updateAppWidget(int, RemoteView)

needs RemoteView

, so the custom one WidgetProvider

has a static method to create this RemoteView

external running instance SmallWidgetProvider

.



Please note that calling AppWidgetManager.updateAppWidget(int, RemoteView)

will not trigger onUpdate()

your custom AppWidgetProvider

.

If you know that every widget will have the same content, you can use AppWidgetManaget.updateAppWidget(int[], RemoteView)

.

0


source







All Articles