我將聯繫人圖片添加到動態小部件。這是我對於這部分代碼:小部件 - 意圖在接收時保持不變
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 != null){
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);
}
我測試的Lookupkey和lookupkey從意向在日誌和它的作品在這一側(可變lookupKey == intent.getStringExtra(ITENT_LOOKUPKEY))。當我現在收到意圖是因爲我點擊了聯繫人圖片,意圖額外的信息總是相同的。無論我點擊哪些聯繫人圖片。這是接收代碼:
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);
}
它始終是第一個添加的聯繫人的lookupKey。在第一個功能中添加另一個聯繫人之前,我是否必須以某種方式清除該意圖,或者存在什麼問題?
感謝您的幫助:) – hitzi