Skip to content Skip to sidebar Skip to footer

How Send Data From Application To Appwidgetprovider?

I am stuck in a particular scenario. I need to get my widget updated as soon as the user update the time from the app. I did try Broadcast by sending the data through Intent Extras

Solution 1:

I do this by adding extras in the intent that I build to update the widget. Also, when the widget has the information it can be passed again into the RemoteViewsService that populates the views for the widget

In my UpdateWidget helper method I pass in the userId (called by various activities in the app that may need to notify the widget that it needs to update itself).

publicstaticvoidupdateWorkoutsWidget(Context context) {
    Intent intent = new Intent(context, WorkoutsWidget.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    intent.putExtra(WorkoutsWidgetService.USER_ID_EXTRA, userId);
    int[] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WorkoutsWidget.class));
    if(ids != null && ids.length > 0) {
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
        context.sendBroadcast(intent);
    }

Then the widget has a field to hold the userId and updates it in onReceive. I pass it into the RemoteViewsService with the Intent that is used to create the collection adapter.

publicclassWorkoutsWidgetextendsAppWidgetProvider {
    privatelong userId;

    @OverridepublicvoidonReceive(Context context, Intent intent) {
        if(intent.getLongExtra(WorkoutsWidgetService.USER_ID_EXTRA, -1) != -1) {
             userId = intent.getLongExtra(WorkoutsWidgetService.USER_ID_EXTRA, -1);
        }
        super.onReceive(context, intent);
    }

    publicvoidonUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // update each of the app widgets with the remote adapterfor (Integer widgetId : appWidgetIds) {
            Intentintent=newIntent(context, WorkoutsWidgetService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            intent.putExtra(WorkoutsWidgetService.USER_ID_EXTRA, userId);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

            RemoteViewsrv=newRemoteViews(context.getPackageName(), R.layout.widget_workouts);
            rv.setRemoteAdapter(R.id.workouts_list, intent);

            rv.setEmptyView(R.id.workouts_list, R.id.empty_view);

            //(Pending intent stuff ommitted for brrevity)

            appWidgetManager.updateAppWidget(widgetId, rv);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

And then all that ends up passing straight on through the RemoteViewsService into its RemoteViewsFactory:

publicclassWorkoutsWidgetServiceextendsRemoteViewsService {
    publicstaticfinalStringTRACK_WORKOUT_ACTION="com.bodybuilding.mobile.service.TRACK_WORKOUT_ACTION";
    publicstaticfinalStringUSER_ID_EXTRA="userId";

    @Overridepublic RemoteViewsFactory onGetViewFactory(Intent intent) {
        returnnewWorkoutWidgetViewsFactory(this.getApplicationContext(), intent);
    }

    classWorkoutWidgetViewsFactoryimplementsRemoteViewsFactory, ServiceConnection {

        WorkoutWidgetViewsFactory(Context context, Intent intent) {
            this.context = context;
            appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
            userId = intent.getLongExtra(USER_ID_EXTRA, -1);
        }

        //Lots of other stuff in here
    }
}

Solution 2:

You have to create a service that updates the widget,check this tutorial (the 4th section), you may try to transfer the data through static variables.

Post a Comment for "How Send Data From Application To Appwidgetprovider?"