当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to keep and access data in BroadcastReceiver?

问题描述:

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.

网友答案:

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.

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: