开发者

Howto in Android: Model inside service, accessed by multiple activities

开发者 https://www.devze.com 2023-01-26 19:42 出处:网络
I have a service that connects to a server (regular TCP socket) and receives data updates in a thread. I want to store this data (after a bit of processing) in a model that can be accessed by multiple

I have a service that connects to a server (regular TCP socket) and receives data updates in a thread. I want to store this data (after a bit of processing) in a model that can be accessed by multiple activities (typically ListView with custom ArrayAdapters). In the activities I use bind to get a handle to the service so that it can access the model and register as a listener to get notifications when the model change.开发者_如何学JAVA

The problem is that access to the model needs to be thread safe. First I thought I would call runOnUIThread with the incoming data inside the service for updating the model, but the service does not have that routine. Any other way to update the model inside service from the UI thread? Or is there a better architecture/pattern that I should use?


No response, but this is what I ended up with: Every on... call (e.g. onCreate, onBind etc) are run on UI thread, so I ended up creating a handler in onCreate and posting to that handler to update my model on the UI thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消