I want to update a ListView in a widget from a service. For this I have this code in my service:
Intent intent = new Intent(ctx, ListProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(widgetId, R.id.listViewWidget, intent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
I'm sure that the service is running and the code is executed but in the widget on the homescreen nothing happens. My ListProvider looks like this:
public class ListProvider implements RemoteViewsFactory
{
public ListProvider(Context context, Intent intent)
{
Log.d("ME", "new ListProvider");
this.context = context;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
populateListItem();
}
private void populateListItem()
{
for (int i = 0; i < 10; i++)
{
ListItem listItem = new ListItem();
listItem.heading = "Heading" + i;
listItem.content = i + " This is the content";
listItemList.add(listItem);
}
}
@Override
public int getCount()
{
return listItemList.size();
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public RemoteViews getViewAt(int position)
{
final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row);
ListItem listItem = listItemList.get(position);
remoteView.setTextViewText(R.id.heading, listItem.heading);
remoteView.setTextViewText(R.id.content, listItem.content);
return remoteView;
}
@Override
public RemoteViews getLoadingView()
{
return null;
}
@Override
public int getViewTypeCount()
{
return 1;
}
@Override
public boolean hasStableIds()
{
return true;
}
// Some other override methods
public class ListItem
{
public String heading,content;
}
}
And this is my AppWidgetProvider:
public class MyWidgetProvider extends AppWidgetProvider
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
But the ListProvider is never called I think. The Log message is not displayed and on the homescreen nothing happens. Any ideas why?