你怎么序列化的Andr​​oid意图?你怎么、意图、序列化、Andr

2023-09-05 07:44:11 作者:嚭

Android的意图有一个方便的 toURI()方法,它的API文档似乎表明是一种序列化的意图的URI,然后再分析它放回了意向。不幸的是,在测试这个功能,我发现它不序列的所有演员,只是基本类型(布尔,整型,长,浮点数,字符串)。如果打算指定任何Parcelable或数组演员那么这些会迷路。

在哪里(如果任何地方)被此限制记录?有一些显而易见的原因,这种行为(我能想象与parcelables的一些困难)?最重要的是,有没有推荐的方式,序列化和解析意图?

我目前的实现只写意图组件(动作,种类,数据的URI,和群众演员),以共享preferences。这种策略不支持parcelables的。

解决方案   

Android的意图有一个方便的方法toURI()该API文档似乎表明是一种更高版本的意图序列化到URI,然后解析它放回的意图。

不是真的。

  

在哪里(如果任何地方),这是限制文档?

我也没有想到群众演员被列入乌里可言。这种乌里背后的点代会向您展示如何添加乌里作为Web上的链接网站,你不应该为这种情况需要临时演员。

toURI()不是序列化。

  

最重要的是,有没有推荐的方式,序列化和解析意图?

没有。尤其是, Parcelable 不能序列,根据定义。

  

我目前的实现只到共享preferences写意图组件(动作,种类,数据URI和演员)。

这种做法简直是匪夷所思了。

使用的数据库。如果你被一个数据库攻击作为一个年轻的孩子,因此住在数据库赤贫的恐惧,使用JSON,XML或序列化 / 的ObjectOutputStream 到一个文件中。使用共享preferences 用户preferences。

  

该策略不支持parcelables的。

也不应该。也不能。 Parcelable 被设计为仅在一个运行装置中的对象图形转换成存储器块以供使用。这不是一个长期的持久性机制。

Android Intents have a convenient toURI() method which the API docs seem to indicate is a way to serialize an Intent to a URI, and then later parse it back into an Intent. Unfortunately, in testing this functionality I found that it does not serialize all extras, just the primitive types (boolean, int, long, float, String). If an intent specifies any Parcelable or array extras then those will get lost.

Where (if anywhere) is this limitation documented? Is there some obvious reason for this behavior (I can imagine some difficulties with Parcelables)? And most importantly, is there a recommended way to serialize and parse Intents?

My current implementation simply writes Intent components (action, categories, data uri, and extras) to a SharedPreferences. This strategy does not support Parcelables.

解决方案

Android Intents have a convenient toURI() method which the API docs seem to indicate is a way to serialize an Intent to a URI, and then later parse it back into an Intent.

Not really.

Where (if anywhere) is this limitation documented?

I would not have expected extras to be included in the Uri at all. The point behind this sort of Uri generation would be to show you how to add the Uri as a link on a Web site, and you should not need extras for that scenario.

toURI() is not serialization.

And most importantly, is there a recommended way to serialize and parse Intents?

No. In particular, Parcelable cannot be serialized, by definition.

My current implementation simply writes Intent components (action, categories, data uri, and extras) to a SharedPreferences.

That approach is simply bizarre.

Use a database. If you were attacked by a database as a young child and therefore live in abject fear of databases, serialize using JSON, XML, or Serializable/ObjectOutputStream to a file. Use SharedPreferences for user preferences.

This strategy does not support Parcelables.

Nor should it. Nor can it. Parcelable is designed to convert an object graph into a memory block for use in a running device only. It is not a long-term persistence mechanism.