How to keep and access data in BroadcastReceiver?

喜你入骨 提交于 2019-12-25 00:57:43

问题


If I understand Android manual, BroadcastReceiver is treated as stateless entity, so I should keep a service which will serve as data container (those are my conclusions).

As per each request for data access I should execute startService (?) -- I can pass this way data to service, but how to get data back?

I mean in recommended way? I could think of passing "out" data, and service on completion would change it, so when startService returns I could check the data I sent in order to retrieve the result.


回答1:


If I understand Android manual, BroadcastReceiver is treated as stateless entity, so I should keep a service which will serve as data container (those are my conclusions).

Having a service around solely to hold onto data will make your users dislike you, and is no guarantee that the data will stick around anyway. The user and the OS can and will get rid of your process whenever they wish.

Please use files (e.g., database, SharedPreferences) for holding data that needs to survive between process invocations.




回答2:


I wouldn't use a service for this, SharedPreferences are very useful for storing some key/value pair data.



来源:https://stackoverflow.com/questions/12858208/how-to-keep-and-access-data-in-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!