RemoteViewsService not called
I'm currently trying to add a ListView
to my widget. Sadly, my RemoteViewsService
does not get called.
Here's my widget refresh code :
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_today_layout);
Intent intent = new Intent(context, TodayWidgetService.class);
intent.putExtra("package", context.getPackageName());
intent.putExtra("items", items.toArray(new String[items.size()])); // items is just a List<String>
views.setRemoteAdapter(R.id.widget_today_content, intent);
manager.notifyAppWidgetViewDataChanged(ids, R.id.widget_today_content);
And the service code (in TodayWidgetService
) :
@Override
public RemoteViewsFactory onGetViewFactory(final Intent intent) {
System.out.println("Called");
return new TodayWidgetReceiver.WidgetFactory(intent.getStringExtra("package"), intent.getStringArrayExtra("items"));
}
As you can see, I print a Called
message each time the service is executing. That's why I'm here : the message isn't in my console (therefore, the service is not called).
I added it in my AndroidManifest.xml
:
<application
...
<service
android:name="services.TodayWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS">
</service>
</application>
Thanks for any help !
Your onUpdate methods needs to be like this one:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// update each of the app widgets with the remote adapter
for (int i = 0; i < appWidgetIds.length; ++i) {
// Set up the intent that starts the StackViewService, which will
// provide the views for this collection.
Intent intent = new Intent(context, StackWidgetService.class);
// Add the app widget ID to the intent extras.
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Instantiate the RemoteViews object for the app widget layout.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
// Set up the RemoteViews object to use a RemoteViews adapter.
// This adapter connects
// to a RemoteViewsService through the specified intent.
// This is how you populate the data.
rv.setRemoteAdapter(R.id.stack_view, intent);
// The empty view is displayed when the collection has no items.
// It should be in the same layout used to instantiate the RemoteViews
// object above.
rv.setEmptyView(R.id.stack_view, R.id.empty_view);
//
// Do additional processing specific to this app widget...
//
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
So check these things:
are you calling the
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
in your refresh code?-
You need to set appWidgetId and data to the Service intent, like so:
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
Refer to the Android Developers docs