0

I have a two part question. Both are somewhat general.

  1. I'm creating an app that relies heavily on communication with a server. I plan to have different classes for each repository I'll need. Is an Android service the correct pattern to use here? There may be certain situations where I'll want to cache things between activities. Will a service allow me to do this?
  2. Assuming a service is what I want to use for this, how can I load content once the service is bound. When the user opens the app, I want to start loading content. However, binding a service isn't blocking, so I can't write the code that makes requests with the service in my onStart() right? Is there some helper class that will wait for the service to load then execute a function? I know I could put some code in my onServiceConnected() method but I'd like to stay away from coupling like that.

Hopefully that wasn't too abstract. Thanks in advance.

MrGrinst
  • 970
  • 3
  • 9
  • 20

3 Answers3

1

1)If you need code to run even when your Activity isn't, the correct answer is a Service. If you just need to cache data, then storing it in a global static variable somewhere may be ok.

2)Your service can start a Thread or AsyncTask. These execute in parallel. onStartCommand generally launches it in this case.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

As with most things, the answer to these questions are subjective at best. I would need more information then I currently have, but I'll take a vague, general stab at this...

  1. If you need something persistently hitting your server repeatedly I would say use a service.

  2. Where you call it is not nearly as important as how many times it needs to be called. That being said the answer is yes. If you need this data as soon as the application or activity loads, then the onCreate method is where it needs to be loaded.

My reccomendation is either A) service or B)AsyncTask.

Go with A if you have to hit the server repeatedly for data and need it in regular intervals. Otherwise go with an AsyncTask and load all the data you need into an object for storage. Then you can use it as you need and it will essentially be "cached".

The difference between the two is simply "best tool for the job". I see you use some javascript. To give a proper analogy, using a service for a server call rather than an async task, is the equivalent of using a web socket (node js) when you could of just used an ajax call. Hope this helps. Oh and PS, please don't use static variables in Android =).

i_me_mine
  • 1,435
  • 3
  • 20
  • 42
1

Yes, Service is the way to go, but a started service, not a bound one.

You could make async request methods, and the Service can broadcast the result back to your Activity.

  • The async request in this case is a startService(intent) with an Intent containing the request parameters. The service would start a background thread for the operation, optimally you can use a networking library for this (for example Volley).
  • And the reply is a broadcast by the Service with the relevant data.

This answers the problem of caching, because the Service can decide what to return. So in case the Service does not have the requested resource, it will download (and return) it. But if the Service has the resource, then it will just simply return the cached version.

To start, you should get yourself familiar with these topics:

I don't know much about your concrete needs, but it seems like you want to implement a REST client with cache. There is a really good Google IO presentation on that here. Definately worth to watch!

kupsef
  • 3,357
  • 1
  • 21
  • 31