Have a look here and scroll to the subheading Adding behavior to individual items.
You need to make sure you call both setPendingIntentTemplate()
from your AppWidgetProvider
and setOnClickFillInIntent()
from your RemoteViewsService.RemoteViewsFactory
implementation.
For example:
public class Widget extends AppWidgetProvider {
// ...
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for(int i = 0; i < appWidgetIds.length; i++){
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent startActivityIntent = new Intent(context, myActivity.class);
PendingIntent startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
widget.setPendingIntentTemplate(R.id.list_view, startActivityPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
// ...
}
}
public class WidgetAdapter implements RemoteViewsService.RemoteViewsFactory {
// ...
@Override
public RemoteViews getViewAt(int position) {
RemoteViews widgetRow = new RemoteViews(context.getPackageName(), R.layout.widget_row);
Intent fillInIntent = new Intent();
fillInIntent.putExtra(Widget.EXTRA_LIST_VIEW_ROW_NUMBER, position);
widgetRow.setOnClickFillInIntent(R.id.list_view_row, fillInIntent);
// ...
return widgetRow;
}
}
There is a more conclusive example in the StackWidget sample which is in the SDK samples, although I found it somewhat difficult to find (see here for directions). It creates an intent to show a Toast message, but it uses the same code.