Android:如何从收集小部件中传递对象到活动?

4

我有一个应用程序的堆栈小部件,目前只是在单击小部件时启动主要活动。我想改变这个,使主要活动可以获得关于被点击的小部件项的信息。我想传递的对象实现了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];
    }
};

好的...你接收到的意图是否与你发送的相同?为什么不尝试将一个操作设置到Intent中,并确认接收到的Intent的操作是否与你发送时设置的操作相同呢?我能想到的唯一原因是你没有接收到你期望接收到的Intent,所以才会得到一个空的“Deal”。 - igor.araujo
似乎存在反序列化问题,因为找不到该类。我尝试设置类加载器,但没有成功。请查看更新。 - bencallis
1个回答

0

你说你已经尝试设置类加载器了。那应该可以工作。我不确定你是如何尝试设置类加载器的,因为你没有提供相关代码。试试这个:

MainActivity 中插入以下代码:

ClassLoader loader = Deal.class.getClassLoader();
queryIntent.setExtrasClassLoader(loader);

在此之前:

Bundle data = queryIntent.getExtras();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接