appwidget 提供者中的 parcelable 对象总是 return null
parcelable object in appwidget provider always return null
在 AppWidgetProvider
的 onReceive()
方法中从 RemoteViewsService
获取可打包对象时,我总是得到 null。
我试过了,通过了 string
和 int
,它工作得很好,但不是 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);
自定义 Parcelable
class 只能由包含该 class 的进程使用。通常,这意味着自定义 Parcelable
class 仅适用于应用程序自己的进程。
由此得出的结论是 there are many places where custom Parcelable
implementations do not work,因为某些 other 进程尝试使用 Parcelable
并失败并返回 ClassNotFoundException
(或同等学历)。
一个这样的模式是当你把一个自定义 Parcelable
作为一个额外的东西放在 Intent
中包裹在 PendingIntent
中,而其他一些进程想要填充那个 [=16] =] 有额外的附加功能。在此过程中,另一个进程将需要访问 Intent
中额外的完整 Bundle
,这将失败,因为另一个进程缺少自定义 Parcelable
。在这种情况下,主屏幕不会有您的自定义 Parcelable
.
或者:
完全跳过此自定义 Parcelable
,或者
跳过自定义 Parcelable
但将一些标识符作为额外内容放入,以便您稍后可以取回 Parcelable
(例如,从缓存中,或者,如果需要,从数据存储重新加载),或
先将其转换成其他格式,例如converting it to a byte[]
,然后再将其放入extra
在 AppWidgetProvider
的 onReceive()
方法中从 RemoteViewsService
获取可打包对象时,我总是得到 null。
我试过了,通过了 string
和 int
,它工作得很好,但不是 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);
自定义 Parcelable
class 只能由包含该 class 的进程使用。通常,这意味着自定义 Parcelable
class 仅适用于应用程序自己的进程。
由此得出的结论是 there are many places where custom Parcelable
implementations do not work,因为某些 other 进程尝试使用 Parcelable
并失败并返回 ClassNotFoundException
(或同等学历)。
一个这样的模式是当你把一个自定义 Parcelable
作为一个额外的东西放在 Intent
中包裹在 PendingIntent
中,而其他一些进程想要填充那个 [=16] =] 有额外的附加功能。在此过程中,另一个进程将需要访问 Intent
中额外的完整 Bundle
,这将失败,因为另一个进程缺少自定义 Parcelable
。在这种情况下,主屏幕不会有您的自定义 Parcelable
.
或者:
完全跳过此自定义
Parcelable
,或者跳过自定义
Parcelable
但将一些标识符作为额外内容放入,以便您稍后可以取回Parcelable
(例如,从缓存中,或者,如果需要,从数据存储重新加载),或先将其转换成其他格式,例如converting it to a
byte[]
,然后再将其放入extra