我的应用程序有一个堆栈小部件,当单击小部件时,它当前只是启动主要活动。我想更改此设置,以便为主要活动提供有关单击的小部件项的信息。我想通过的对象实现 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;
}
主要活动
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 寓言已在应用程序的主要活动中成功使用,我只是在小部件上遇到了这个问题。
交易类
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];
}
};