I have an AppWidget that has a StackView contained within it. Along with the AppWidget, I have a service that is started when a user adds my AppWidget to their homescreen that polls for new data every two hours. When the service finds new data, I need for it alert my AppWidget, and also pass that data to the RemoteViewsService (which has my RemoteViewsFactory) so that it can create the necessary views for the new data.
Currently, when the service finds new data, I have it broadcast to my AppWidget that new data has been found, passing that data as an array of integers within an intent.
My onReceive method in my AppWidgetProvider pulls that data out, and I then go through the process of setting up a new intent that is passed to my RemoteViews for the AppWidget. Sample code below:
public void onReceive( Context context, Intent intent )
{
int[] appWidgetIds = appWidgetManager.getAppWidgetIds( new ComponentName( context, SampleAppWidgetProvider.class ) );
int[] sampleData = intent.getIntArrayExtra( Intent.EXTRA_TITLE );
for ( int i = 0; i < appWidgetIds.length; i++ )
{
// RemoteViewsFactory service intent
Intent remoteViewsFactoryIntent = new Intent( context, SampleAppWidgetService.class );
remoteViewsFactoryIntent.putExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i] );
remoteViewsFactoryIntent.setData( Uri.parse( remoteViewsFactoryIntent.toUri( Intent.URI_INTENT_SCHEME ) ) );
remoteViewsFactoryIntent.putExtra( Intent.EXTRA_TITLE, sampleData );
RemoteViews rv = new RemoteViews( context.getPackageName(), R.layout.widget_layout );
// Sync up the remoteviews factory
rv.setRemoteAdapter( appWidgetIds[i], R.id.sample_widget, remoteViewsFactoryIntent );
rv.setEmptyView( R.id.sample_widget, R.id.empty_view );
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
super.onUpdate( context, appWidgetManager, appWidgetIds );
}
This works the first time. The data is displayed by the RemoteViewsService/RemoteViewsFactory. Subsequent new data doesn't hit the RemoteViewsFactory's constructor because the service for that factory is already running.
How do I go about updating the data? I feel like I should be using onDataSetChanged, but how would I access the intent that I passed from the AppWidget's onReceive?
I'd appreciate any insight as to how to go about this properly. Thanks.