开发者

Widget - Intent stays the same when receiving

开发者 https://www.devze.com 2023-03-16 23:49 出处:网络
I add contact pictures to a widget dynamical. This is my code for this part: for (int x = 0; x < this.appWidgetIds.length; x++){

I add contact pictures to a widget dynamical. This is my code for this part:

for (int x = 0; x < this.appWidgetIds.length; x++){
        int id = this.appWidgetIds[x];
        RemoteViews rv = new RemoteViews(this.context.getPackageName(), R.layout.widget);
        for (int i = 0; i < maxCount; i++){
            String lookupKey = sortedItems.get(i).getLookupKey();
            Tools.ToLog("LOOKUPKEY=" + lookupKey);
            Bitmap bmp = Contact.getContactPicture(this.context, lookupKey);
            if (bmp != n开发者_运维问答ull){
                Intent intent = new Intent(context, ContactsWidget.class);
                intent.setAction(ACTION_WIDGET_RECEIVER);
                intent.putExtra(ITENT_LOOKUPKEY, lookupKey);
                Tools.ToLog("LOOKUPKEY - IDENT=" + intent.getStringExtra(ITENT_LOOKUPKEY));

                RemoteViews itemView = new RemoteViews(this.context.getPackageName(), R.layout.widget_itemview);
                itemView.setImageViewBitmap(R.id.widget_ImageView, bmp);
                PendingIntent actionPendingIntent = PendingIntent.getBroadcast(this.context, 0, intent, 0);
                itemView.setOnClickPendingIntent(R.id.widget_ImageView, actionPendingIntent);
                rv.addView(R.id.widgetContainer, itemView);
            }
        }
        appWidgetManager.updateAppWidget(id, rv);
    }

I tested the Lookupkey and the lookupkey from the intent over the log and it works on this side (variable lookupKey == intent.getStringExtra(ITENT_LOOKUPKEY)). When I now receive the intent because I clicked on a contact picture the intent extra info is always the same. No matter which of the contact pictures I clicked. This is the receive code:

public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
        String lookupKey = intent.getStringExtra(ITENT_LOOKUPKEY);
        Toast.makeText(context, "Lookup Key: " + lookupKey, Toast.LENGTH_SHORT).show();
        //Contact.openContact(this.context, lookupKey);             
    }
    super.onReceive(context, intent);
}

It's always the lookupKey from the first added contact. Do I have to clear the intent somehow before adding another contact in the first function or what is the problem?


You only have one PendingIntent.

Quoting the documentation:

If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid

Since you have the same operation (getActivity()) and the same Intent routing pieces each time, there is only one PendingIntent.

Rather than setting the action to be ACTION_WIDGET_RECEIVER, make it be unique for each that you are creating in your loops.

0

精彩评论

暂无评论...
验证码 换一张
取 消