如何在手机上本地保存待处理的意图

时间:2016-05-29 15:18:16

标签: android android-pendingintent

我需要存储从StatusBarNotification对象获取的PendingIntent对象。 PendingIntent pendingIntent = sbn.getNotification()。contentIntent;

4 个答案:

答案 0 :(得分:0)

PendingIntent可以序列化为字节流,因为它是Parcelable。创建Parcel,在writeToParcel()上致电PendingIntent,将对象序列化为Parcel,然后在marshall()上致电Parcel以获取字节,然后您可以写入设备上的文件。

注意:Parcelable接口不是为长期持久性而设计的,因此这可能并不适用于所有情况。这取决于你想用它做什么。

答案 1 :(得分:0)

阅读:

PendingIntent pi = PendingIntent.readPendingIntentOrNullFromParcel(parcel);

写信给:

PendingIntent.writePendingIntentOrNullToParcel(PendingIntent,parcel);

编辑1:

  

包裹无法保存吗? @ ceph3us - Terrel Lewis

你可以"保存" Parcelable或Parcel

Parcelable -----> 
         Parcel.obtain() ----->
                Parcelable.writeToParcel(Parcel,0) ----->  // write to parcel 
                     Parcel.marshal() ----- >  byte[]  

然后字节可以保存到文件数据库或任何其他持久性或非数据存储。你可以做的一件事是编组包含IBinder对象的parcelabl阅读更多:

在编组对象类之前,需要删除binder - binder引用存储在内核空间中。如果binder来自你,你应该很容易创建一个新的,并在以后分配给未编组的数据。

答案 2 :(得分:0)

适用于自己生成的 PendingIntents的解决方案(防止" java.lang.RuntimeException:尝试编组包含Binder对象的Parcel。"):

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);

答案 3 :(得分:0)

如果您想在服务正在运行时存储暂挂意图.....使用哈希映射并将密钥存储在数据库中,这等效于服务中的暂挂意图和make方法,以返回哈希映射....使用键,如果持久存在,您可以轻松访问它。