保存在广播接收器数据接收器、存在、数据

2023-09-05 01:01:11 作者:一见钟情再见陌路,

我想保持一个哈希表中的广播接收机。如果我理解的BroadcastReceiver的生命周期目前它可能丧命消灭我的成员变量。什么是从的onReceive中的BroadcastReceiver一个previous运行检索哈希表理想的策略是什么?

I'd like to maintain a hash table in a broadcast receiver. If I understand BroadcastReceiver's life cycle currently it could get killed wiping out my member variables. What would be the ideal strategy for retrieving a hash table from a previous run of onReceive in the BroadcastReceiver?

推荐答案

有使用两种方法的BroadcastReceiver ,而你没有说明你正在使用。

There are two ways to use a BroadcastReceiver, and you did not indicate which you are using.

一个用于其他一些组件注册一个接收器 - 就像一个活动 - 通过 registerReceiver()。该接收器将活只要注册,因此它的数据可能会持续超过一个的onReceive()通话。已注册接收器的组件将负责持久化数据。

One for a receiver registered by some other component -- like an Activity -- via registerReceiver(). This receiver will live for as long as it is registered, and so its data may last for more than one onReceive() call. The component that registered the receiver would be responsible for persisting the data.

另一种是注册您的接收器清单。这些,每在cdonner的答案引文,将一个的onReceive()通话后自行消失。您的接收器将需要保存其数据本身,到数据库,平面文件,或什么的。

The other is to register your receiver in the manifest. Those, per the quoted passage in cdonner's answer, will go away after a single onReceive() call. Your receiver will need to persist its data itself, to a database, flat file, or whatever.