【问题标题】:parcelable object in appwidget provider always return nullappwidget 提供程序中的 parcelable 对象始终返回 null
【发布时间】:2016-10-21 19:46:01
【问题描述】:

当在AppWidgetProvideronReceive() 方法中从RemoteViewsService 获取parcelable 对象时,我总是得到null

我试过了,通过了stringint,它工作得很好,但不是parcelable

StackWidgetProvider.java 扩展了 AppWidgetProvider

@Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(INTENT_ACTION)){

            Artwork artwork = (Artwork) intent.getParcelableExtra(EXTRA_ART);
            Log.e("Intent","->"+artwork.getTitle());
            Intent showArtDetail = new Intent(context, ArtsDetailsActivity.class);
            showArtDetail.putExtra(ArtsDetailsActivity.TAG_ARTWORK, artwork);
            showArtDetail.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(showArtDetail);
        }

        super.onReceive(context, intent);
    }

StackWidgetService.java 扩展 RemoteServiceView

Bundle extras = new Bundle();
extras.putParcelable(StackWidgetProvider.EXTRA_ART, artwork);

Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
remoteViews.setOnClickFillInIntent(R.id.widget_item, fillInIntent);

【问题讨论】:

标签: android android-appwidget appwidgetprovider


【解决方案1】:

自定义Parcelable 类只能由包含该类的进程使用。通常,这意味着自定义 Parcelable 类仅适用于应用程序自己的进程。

对此的推论是there are many places where custom Parcelable implementations do not work,因为一些其他 进程尝试使用Parcelable 并以ClassNotFoundException(或等效项)失败。

一个这样的模式是,当您将自定义 Parcelable 作为额外的 Intent 包裹在 PendingIntent 中时,并且其他一些进程想要用额外的额外填充 Intent。在此过程中,其他进程将需要访问Intent 中的完整Bundle,这将失败,因为其他进程缺少自定义Parcelable。在这种情况下,主屏幕不会有您的自定义Parcelable

要么:

  • 直接跳过此自定义Parcelable,或

  • 跳过自定义的Parcelable,但添加一些标识符作为额外的,这将允许您稍后重新获取Parcelable(例如,从缓存中,或者,如果需要,从数据存储中重新加载) , 或

  • 先将其转换为其他格式,例如converting it to a byte[],然后再放入额外文件中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 2012-10-16
    • 1970-01-01
    相关资源
    最近更新 更多