开发者

API RemoteService confusions

开发者 https://www.devze.com 2023-01-13 17:28 出处:网络
I\'m trying to architect a REST-heavy application. Following one of the models proposed by Virgil in his Google I/O presentat开发者_高级运维ion, I\'ve set up my application like so:

I'm trying to architect a REST-heavy application.

Following one of the models proposed by Virgil in his Google I/O presentat开发者_高级运维ion, I've set up my application like so:

My Activity calls upon a "Service Helper" which is a singleton. This singleton instantiates a Service which, upon start, uses a ContentProvider to hit my application's database and spins a thread to hit a web service for any pertinent syncing. The last piece I now need is having the Service callback to the singleton, which calls back to any listening Activities.

This is the part I'm confused about.

Right now, I'm looking at the Android SDK's provided RemoteService ApiDemo. In RemoteServiceBinding.java, an IRemoveService member var is declared called mService. mService is used in various handlers to handle the callbacks.

However, this code will go in my Service Helper (remember this is a singleton). There could be N number of calls to this class to start up Services for database and web service functions. I can't just declare 1 variable called mService, right? The RemoteService declares an ISecondaryService, but I could have N number of pieces of code calling this singleton. It needs to be dynamic.

Ex:

  • User is on Activity A and wants to show things in a list. The Service Helper singleton is hit, returns a Cursor and starts a Service which is querying a web service for new things. mService is instantiated to handle callbacks for this Service instance.
  • Before this Service is finished, the user switches to Activity B and does the same thing for the things in that Activity's list. mService is already instantiated for the previous Service instance. What do I do now?
  • Suppose the connection was slow and the user could do this for 20 Activities before the first one finished getting new things. mService is already instantiated for the previous Service instance. What do I do now?????

What do I do here? I'm at a loss.

It would be really really nice if the Twitter app was open sourced already...


I don't know where you ended up on this, but if you used an IntentService, only one Service would need to be created, and that IntentService would place new calls to Context#startService(Intent i) in a queue.

As the single IntentService handles the queue, they will be processed in the onHandleIntent(Intent i) method of your IntentService. In this method you could to pull extras from the passed Intent to figure out exactly how to handle the requirements set forth by the calling Activity.

0

精彩评论

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

关注公众号