保存在广播接收器数据 [英] Save data in Broadcast receiver

查看:168
本文介绍了保存在广播接收器数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保持一个哈希表中的广播接收机。如果我理解的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.

这篇关于保存在广播接收器数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆