我有一个应用程序的堆栈小部件,目前只是在单击小部件时启动主要活动。我想改变这个,使主要活动可以获得关于被点击的小部件项的信息。我想传递的对象实现了Parcelable接口,并且可以通过通知中的意图成功传递。然而,当从小部件捆绑到意图中时,它似乎总是为空。
有什么想法吗?
StackWidgetProvider
// set intent for item click (opens main activity)
Intent viewIntent = new Intent(context, DealPadActivity.class);
viewIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
viewIntent.setData(Uri.parse(viewIntent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.stack_view, viewPendingIntent);
StackWidgetService
public RemoteViews getViewAt(int position) {
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.stack_widget_item);
if (position < mWidgetItems.size()){
rv.setTextViewText(R.id.stack_widget_title, mWidgetItems.get(position).title);
rv.setTextViewText(R.id.stack_widget_price, mWidgetItems.get(position).price);
rv.setTextViewText(R.id.stack_widget_heat, mWidgetItems.get(position).heat);
Bitmap picture = imageLaoder.getBitmap(mWidgetItems.get(position).imageUrl, true) ;
rv.setImageViewBitmap(R.id.stack_widget_image, picture);
}
Intent fillInIntent = new Intent();
Bundle extras = new Bundle();
extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
extras.putParcelable("notificationDeal", new Deal(...));
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.stack_widget_layout, fillInIntent);
return rv;
}
MainActivity
Bundle data = queryIntent.getExtras();
if (data!=null){ // check to see if from widget or notification
Deal deal = data.getParcelable("notificationDeal");
if (deal!=null){ // ****ALWAYS NULL FROM WIDGET****
Log.d(this.getClass().getSimpleName(), "going to show passed deal");
onDealSelected(deal);
}
int i = data.getInt("com.bencallis.dealpad.stackwidget.EXTRA_ITEM");
Log.d(this.getClass().getSimpleName(), "pressed widget is: " + i); **WIDGET ITEM POSITION AS EXPECTED **
** 更新 **
看起来这是一个与编组(尤其是非编组)相关的问题。
E/Parcel: Class not found when unmarshalling: com.bencallis.abc.Deal, e: java.lang.ClassNotFoundException: com.bencallis.abc.Deal
我已经将Deal类导入StackWidgetService中,并尝试设置类加载器,但似乎没有帮助。
Deal寓言在应用程序的主要活动中成功使用,我只是在小部件中遇到了这个问题。
Deal类
public Deal(Parcel in) {
in.readParcelable(Deal.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
...
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Deal createFromParcel(Parcel in) {
return new Deal(in);
}
public Deal[] newArray(int size) {
return new Deal[size];
}
};