0

I'm developing an Android widget for displaying contacts, but I can't create a eventClick when I click on the contact image.

I use a StackRemoteViewsFactory, for example to add the contacts to my GridView.

Here is my code:

public RemoteViews getViewAt(int position) {

    Bitmap photo = loadContactPhoto(mContext.getContentResolver(), mWidgetItems.get(position).id);

    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.contact_item);
    rv.setTextViewText(R.id.name, mWidgetItems.get(position).name);
    if (photo != null) {
        rv.setImageViewBitmap(R.id.image, photo);
    }

    final Intent fillInIntent = new Intent();
    final Bundle extras = new Bundle();
    extras.putLong(FavoriteContactsWidgetProvider.CONTACT_ID, mWidgetItems.get(position).id);
    fillInIntent.putExtras(extras);
    rv.setOnClickFillInIntent(R.id.contact_frame, fillInIntent);

    return rv;
}

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int i = 0; i < appWidgetIds.length; ++i) {
        final Intent intent = new Intent(context, FavoriteContactsWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);

        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        rv.setRemoteAdapter(appWidgetIds[i], R.id.grid_view, intent);

        final Intent onClickIntent = new Intent(context, FavoriteContactsWidgetProvider.class);

        onClickIntent.setAction(FavoriteContactsWidgetProvider.CLICK_ACTION);
        onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        onClickIntent.setData(Uri.parse(onClickIntent.toUri(Intent.URI_INTENT_SCHEME)));

        final PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        rv.setPendingIntentTemplate(R.id.image, onClickPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

Anyone have some ideas to make this work?

patricus
  • 59,488
  • 15
  • 143
  • 145

2 Answers2

2

pay attention this line:

 rv.setPendingIntentTemplate(R.id.image, onClickPendingIntent);

i think u should replace R.id.image as R.id.grid_view,because the docs of Google has explained the viewId as "The id of the collection who's children will use this PendingIntent template when clicked" in the method setPendingIntentTemplate (int viewId, PendingIntent pendingIntentTemplate),

Abner Niu
  • 405
  • 3
  • 7
1

You need to receive the pending intents broadcast in you WidgetProvider onRecieve() method.

Also make sure you have that broadcast action as an intent-filter in the WidgetProvider entry (receiver) of your android manifest.

micwallace
  • 480
  • 5
  • 13