Persistent services/thread in android
I've just recently started working with the android framework and am looking for advice/design patterns to use for the following situation:
Application is a server/client structure. The server should be running at all times (or at least be recreated shortly after termination), but the client can close as need be. To accomplish the server, I understand that I want to extend Service
and use START_STICKY
functionality and run it in the background.
The part where I'm a bit lost of how to proceed is that I want to create several persist开发者_JAVA技巧ent network connections inside the server. It is important that the connections are threaded and can run asynchronously. A lot of documents point towards using AsyncTask
for this, but I don't need to reference the UI thread at all from the network threads and the connections wont ever be terminated, so I'm hoping someone could point me in a better direction?
My conceptual idea was to have the network threads bind to the service to store events, and the client can then also bind to the service to retrieve the events. I'm unsure of the structure to use for these network threads.
The server should be running at all times
There are very very few applications that need this. Statistically speaking, yours probably isn't one of them. Users attack people like you with task killers and the like, plus give out one-star ratings on the Market for harming their use of their device. I strongly encourage you to reconsider this design decision.
so I'm hoping someone could point me in a better direction?
Plain old ordinary Java threads are just fine. Depending on the nature of the threads, you might consider using a thread pool. This part is not significantly different than in other Java environments. Bear in mind that the vast majority of Android devices today are single-core, and you only have so much heap space (as little as 16MB), so having tons o' threads isn't exactly great.
My conceptual idea was to have the network threads bind to the service to store events
The network threads are part of the service, and therefore don't need to bind to it.
精彩评论