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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
One possible solution is to make this map static. This seems ok for Receivers registered in the manifest, since there is only one receiver at a time.
Same trick might be used to register a Handler to receive feedback from Receiver.
Your best strategy would be to use a database to store your data in a table instead of any type of in memory Map.
That way it wouldn't matter if the user turned off and then turned the phone back on again, your data from previous calls would still be available.
There are two ways to use a
BroadcastReceiver
, and you did not indicate which you are using.One for a receiver registered by some other component -- like an
Activity
-- viaregisterReceiver()
. This receiver will live for as long as it is registered, and so its data may last for more than oneonReceive()
call. The component that registered the receiver would be responsible for persisting the data.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.downcast the data to Object class and then up-cast to required class object :-)
From the Android Reference:
Does not sound like what you want to do will work. The Actitivy that registers the receiver will have to take care of this, or you could persist your hash table to the database.