I'm using widget with listview that has 3 types of items. For each type of item I should use different pending intents. Currently I'm using following code:
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager widgetManager, int[] widgetIds) {
for (int widgetId : widgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_view);
bindEmptyView(remoteViews);
bindRemoteAdapter(context, widgetId, remoteViews);
bindIntentTemplate(context, widgetId, remoteViews);
widgetManager.updateAppWidget(widgetId, remoteViews);
}
}
private void bindEmptyView(RemoteViews remoteViews) {
remoteViews.setEmptyView(android.R.id.list, android.R.id.empty);
}
private void bindRemoteAdapter(Context context, int widgetId, RemoteViews remoteViews) {
Intent intent = new Intent(context, MyViewService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(android.R.id.list, intent);
}
private void bindIntentTemplate(Context context, int widgetId, RemoteViews remoteViews) {
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent template = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(android.R.id.list, template);
}
}
From this point I don't understand how can I invoke specific intent for different list items.
Hopefully I understood your issue well, so I'll try to explain briefly what's happening and how to handle clicks on list items on the widget.
I assume you already know that you have to implement a class which:
This will serve as the "adapter" for your widget's ListView(let's call it MyListRemoteViewFactory). If you want to handle item clicks on a widget's listView, you do the following things:
1) set a
setPendingIntentTemplate
in your AppWidgetProvider class2) set a
setOnClickFillInIntent
in the MyListRemoteViewFactory overriddengetViewAt(int position)
methodNOW: Doing step 1), you might wanna do something like:
You can place the above code snippet after wherever you initialize your RemoteViews object:
So right now you have your pendingIntentTemplate ready. One other thing to do is to implement the class'
onReceive
method, so you can decide what to do when an action for the above case occurred. So you'll do something like:[Damn this'll be long]
DOING STEP 2) (from now on we talk about implementations in the MyListRemoteViewFactory class)
If you need 3 different items in the list, first you have to add this method to the MyListRemoteViewFactory(you're forced to override it anyway, the key is to return the number of views you have):
In the
getViewAt()
method you add your logic based on what, you decide what to display depending on your position. Something like:And the
setUpItem
method might look something like:You might want to make sure you declared everything in the Manifest file as well. You have to declare your widgetprovider, your receiver for the list, and a service handling your Factoryclass. You should have something like:
Ah, BTW the WidgetRemoteViewsService class should look like this:
I guess this is pretty much it. I hope I didn't skip anything.