2011-07-04 39 views
1

我將聯繫人圖片添加到動態小部件。這是我對於這部分代碼:小部件 - 意圖在接收時保持不變

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。在第一個功能中添加另一個聯繫人之前,我是否必須以某種方式清除該意圖,或者存在什麼問題?

回答

2

您只有一個PendingIntent

引用文檔:

如果創建的應用程序以後重新取回同種的PendingIntent的(相同的操作,目的相同的動作,數據,類別和組件,以及相同的標誌),它將接收代表同樣的道理,如果這是仍然有效

既然你有相同的操作(getActivity())和每次一樣Intent路由枚的PendingIntent,只有一個PendingIntent

而不是將動作設置爲ACTION_WIDGET_RECEIVER,使其對於您在循環中創建的每個動作都是唯一的。

+0

感謝您的幫助:) – hitzi