Java 使用android在SQLite数据库中存储in-Intent

Java 使用android在SQLite数据库中存储in-Intent,java,android,sqlite,android-intent,Java,Android,Sqlite,Android Intent,我有一个android appand,我想在数据库中将android意图存储为blob字段。我知道在SQLite中存储和检索文本、整数等标准类型数据的基础知识,因为我的应用程序已经完成了所有这些。我不熟悉存储和检索blob。我认为blob是最好的方法,而不是将其存储为字符串并解析回和意图。也许这不是一个正确的假设。意图可以是包名、URI、可能有额外内容等。这就是为什么我希望存储/检索整个意图,而不仅仅是存储部分。使用Parcelable界面来实现以下意图: Parcel parcel; m

我有一个android appand,我想在数据库中将android意图存储为blob字段。我知道在SQLite中存储和检索文本、整数等标准类型数据的基础知识,因为我的应用程序已经完成了所有这些。我不熟悉存储和检索blob。我认为blob是最好的方法,而不是将其存储为字符串并解析回和意图。也许这不是一个正确的假设。意图可以是包名、URI、可能有额外内容等。这就是为什么我希望存储/检索整个意图,而不仅仅是存储部分。

使用Parcelable界面来实现以下意图:

Parcel  parcel;  
myIntent.writeToParcel(parcel);

ContentValues values = new ContentValues();  
values.put(MyField, parcel.createByteArray());  
getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);
String intentDescription = cursor.getString(intentIndex);    
Intent intent = Intent.parseUri(intentDescription, 0);
把这个过程倒过来把它弄出来


如果你喜欢文本,那么你可以考虑使用JSON.

< P>你可以简单地用字符串的方式存储意图:

String intentDescription = intent.toUri(0);
//Save the intent string into your database
稍后,您可以恢复意图:

Parcel  parcel;  
myIntent.writeToParcel(parcel);

ContentValues values = new ContentValues();  
values.put(MyField, parcel.createByteArray());  
getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);
String intentDescription = cursor.getString(intentIndex);    
Intent intent = Intent.parseUri(intentDescription, 0);

这是一个非常糟糕的主意。来自Android文档:“Parcel不是一种通用的序列化机制。此类(以及用于将任意对象放入Parcel的相应Parcelable API)设计为高性能IPC传输。因此,不适合将任何包裹数据放入永久性存储中:包裹中任何数据的基础实现中的更改都可能导致旧数据无法读取。“不幸的是,这不会存储标志和所有额外内容。@JacksOnF1re这不是真的。请阅读。“URI包含作为基本URI的意图数据,还有一个描述操作、类别、类型、标志、包、组件和附加内容的附加片段。”Intent.parseUri()的性能不是很好(它调用String.starts的次数太多)。