我需要存储从StatusBarNotification对象获取的PendingIntent对象。 PendingIntent pendingIntent = sbn.getNotification()。contentIntent;如何在手机上本地保存待处理的意图
回答
甲PendingIntent
可以被序列化到一个字节流,因为它是Parcelable
。在PendingIntent
上创建一个Parcel
,请拨writeToParcel()
将对象序列化为Parcel
,然后在Parcel
上调用marshall()
以获取字节,然后可以将该字节写入设备上的文件。
注:Parcelable
接口并没有设计为长期持久性,所以这可能并不适用于所有情况。这取决于你想使用它。
感谢@DavidWasser的回应。我尝试过这种方法,但遇到了一个错误:尝试编制一个包含Binder对象的包裹。我如何克服这一点? –
对不起,你不能。有些东西可以放入无法序列化的'Parcel'中。你为什么要这样做?也许我们可以找到另一种方法来解决你的问题。 –
我需要保存我从其他应用程序的通知中收到的pendingintent,以便能够稍后从我的应用程序启动它们。@DavidWasser –
阅读:
PendingIntent pi = PendingIntent.readPendingIntentOrNullFromParcel(parcel);
写:
PendingIntent.writePendingIntentOrNullToParcel(PendingIntent,parcel);
编辑1:
Parcels cannot be saved right? @ceph3us – Terrel Lewis
您可以 “保存” Parcelable或包裹
Parcelable ----->
Parcel.obtain() ----->
Parcelable.writeToParcel(Parcel,0) -----> // write to parcel
Parcel.marshal() ----- > byte[]
然后字节可以通过保存到文件数据库或任何其他持久存储或不存储数据。一件事,你可以做的是马歇尔包含的IBinder对象parcelabl阅读更多:
你之前马歇尔,你需要删除粘合剂中的对象类 - 活页夹引用存储在内核空间中。如果粘结剂来自于你,你应该很容易地创建一个新的,并在稍后分配给未编组的数据。
包裹无法正确保存吗? @ ceph3us –
这是工作为自己产生 PendingIntents溶液(防止 “了java.lang.RuntimeException:试过马歇尔包含粘结剂对象包”。):
public class PersistentPendingIntent implements Parcelable
{
private enum PendingIntentType{SERVICE, BROADCAST, ACTIVITY}
@NonNull
private final PendingIntentType pendingIntentType;
protected final int requestCode;
protected final int flags;
@NonNull
protected final Intent intent;
private PersistentPendingIntent(@NonNull PendingIntentType pendingIntentType, int requestCode, @NonNull Intent intent, int flags)
{
this.pendingIntentType = pendingIntentType;
this.flags = flags;
this.intent = intent;
this.requestCode = requestCode;
}
@Nullable
public PendingIntent getPendingIntent(@NonNull Context context)
{
PendingIntent pendingIntent = null;
switch (pendingIntentType)
{
case SERVICE:
pendingIntent = PendingIntent.getService(context, requestCode, intent, flags);
break;
case BROADCAST:
pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, flags);
break;
case ACTIVITY:
pendingIntent = PendingIntent.getActivity(context, requestCode, intent, flags);
break;
}
return pendingIntent;
}
public static PersistentPendingIntent getService(int requestCode, @NonNull Intent intent, int flags)
{
return new PersistentPendingIntent(PendingIntentType.SERVICE, requestCode, intent, flags);
}
public static PersistentPendingIntent getActivity(int requestCode, @NonNull Intent intent, int flags)
{
return new PersistentPendingIntent(PendingIntentType.ACTIVITY, requestCode, intent, flags);
}
public static PersistentPendingIntent getBroadcast(int requestCode, @NonNull Intent intent, int flags)
{
return new PersistentPendingIntent(PendingIntentType.BROADCAST, requestCode, intent, flags);
}
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
dest.writeInt(this.pendingIntentType.ordinal());
dest.writeInt(this.requestCode);
dest.writeInt(this.flags);
dest.writeParcelable(this.intent, flags);
}
protected PersistentPendingIntent(Parcel in)
{
int tmpPendingIntentType = in.readInt();
this.pendingIntentType = PendingIntentType.values()[tmpPendingIntentType];
this.requestCode = in.readInt();
this.flags = in.readInt();
this.intent = in.readParcelable(Intent.class.getClassLoader());
}
public static final Creator<PersistentPendingIntent> CREATOR = new Creator<PersistentPendingIntent>()
{
@Override
public PersistentPendingIntent createFromParcel(Parcel source)
{
return new PersistentPendingIntent(source);
}
@Override
public PersistentPendingIntent[] newArray(int size)
{
return new PersistentPendingIntent[size];
}
};
}
用法:
PersistentPendingIntent persistentPendingIntent = PersistentPendingIntent.getService(REQUEST_CANCEL, cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Serialization
Parcel parcel = Parcel.obtain();
parcel.writeValue(persistentPendingIntent);
byte[] serializedPersistentPendingIntent = parcel.marshall();
parcel.recycle();
// Deserialization
parcel.unmarshall(serializedPersistentPendingIntent, 0, serializedPersistentPendingIntent.length);
parcel.setDataPosition(0);
persistentPendingIntent = (PersistentPendingIntent) parcel.readValue(PersistentPendingIntent.class.getClassLoader());
PendingIntent pendingIntent = persistentPendingIntent.getPendingIntent(context);
- 1. 如何将上传的文件保存在本地主机上?
- 2. 试图创建排行榜并在本地保存到手机
- 3. 如何在本地(在本机上)保存JSON数据?
- 4. 保存图像在本地计算机
- 5. 如何在手机上使用本地存储?
- 6. 如何在Silverlight 4.0中的本地机器上保存文件?
- 7. 手机本地存储?
- 8. 如何在本地保存highchart图形?
- 9. 如何将超级景观图像保存到本地手机相册
- 10. 我该如何取消所有待处理意图的服务
- 11. 如何保存意图
- 12. 如何在iphone上保存谷歌地图上的图像
- 13. 如何将位图保存到手机的图库中?
- 14. 如何访问ASP.NET本地主机网站上的Android手机
- 15. 在手机上播放本地声音
- 16. 在本地网络上测试手机
- 17. 得到保存在手机
- 18. 如何在手机上加载本地主机上的php文件
- 19. 在AppWidgets中处理(等待)意图的正确方式
- 20. 每次在Android中创建新的待处理意图
- 21. 我如何保存在本地存储
- 22. 如何将图片保存到手机图库
- 23. J2ME:从服务器下载图像并在手机上保存
- 24. 相机意图图片只保存在相机相册
- 25. 如何在批处理中保存?
- 26. 我应该如何着手处理使用MVC3的本地化?
- 27. 来自服务的待处理意向
- 28. 批处理脚本保存在windows
- 29. 如何在手机上查看本地网站?
- 30. 如何在iphone和android手机上运行本地html文件?
见http://stackoverflow.com/questions/6661268/marshalling-a-notification-parcel –
这被张贴的方式回到2012年有似乎仍不是一个具体的解决了这一点。解决这个问题会有趣,有什么说@DavidWasser? –