2016-05-29 97 views
1

我需要存储从StatusBarNotification对象获取的PendingIntent对象。 PendingIntent pendingIntent = sbn.getNotification()。contentIntent;如何在手机上本地保存待处理的意图

+0

见http://stackoverflow.com/questions/6661268/marshalling-a-notification-parcel –

+0

这被张贴的方式回到2012年有似乎仍不是一个具体的解决了这一点。解决这个问题会有趣,有什么说@DavidWasser? –

回答

0

PendingIntent可以被序列化到一个字节流,因为它是Parcelable。在PendingIntent上创建一个Parcel,请拨writeToParcel()将对象序列化为Parcel,然后在Parcel上调用marshall()以获取字节,然后可以将该字节写入设备上的文件。

注:Parcelable接口并没有设计为长期持久性,所以这可能并不适用于所有情况。这取决于你想使用它。

+0

感谢@DavidWasser的回应。我尝试过这种方法,但遇到了一个错误:尝试编制一个包含Binder对象的包裹。我如何克服这一点? –

+0

对不起,你不能。有些东西可以放入无法序列化的'Parcel'中。你为什么要这样做?也许我们可以找到另一种方法来解决你的问题。 –

+0

我需要保存我从其他应用程序的通知中收到的pendingintent,以便能够稍后从我的应用程序启动它们。@DavidWasser –

0

阅读:

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阅读更多:

你之前马歇尔,你需要删除粘合剂中的对象类 - 活页夹引用存储在内核空间中。如果粘结剂来自于你,你应该很容易地创建一个新的,并在稍后分配给未编组的数据。

+0

包裹无法正确保存吗? @ ceph3us –

0

这是工作为自己产生 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);