Lisa Anne
Lisa Anne

Reputation: 4595

How to implement communication Activity-Service

I have the following situation:

I have a Service that checks periodically for new data over the internet,

The user might want to request an immediate update...

...in that case I use a Messenger to request the Service to look for new data

Here is the problem:

the user is notified that a request is ongoing, but it might take a while, can be unsuccessful, could never return...

currently I get a message (using a Messenger) back from the Service to the Activity informing of the result of the request, or, if I get no message, in x seconds I inform the user that the request was unsuccessful.

  1. Please can you suggest a different approach?
  2. I don't like to wait for a message and if after x seconds none is received inform the user, is there a better way?

Upvotes: 7

Views: 261

Answers (3)

Ilan Klinghofer
Ilan Klinghofer

Reputation: 890

If you think about this as a Model View Controller problem, the issue here is the lack of a model to represent the state of the Service. When the Service is performing the refresh, this "state" needs to reflected in your UI. Thus the Service needs to record this somewhere that the UI can access.

One option is simply a piece of shared memory, such as a Singleton object or even a static member variable (not recommended). Another option is to keep that state in your database.

Another issue is notifying the UI when this state changes. As mentioned by other posts there are multiple ways of doing this such as a LocalBroadcast, message bus like Otto, ContentObservers, etc.

Upvotes: 1

rupesh jain
rupesh jain

Reputation: 3430

You might consider optimistic rendering/optimistic updates- A pattern in which you update the UI on client as if it has being successful on the server. Once you get response from the server you update the UI accordingly.You can refer apps with new designs like google hangouts.

For more info refer this discussions:

I guess using this approach will give better usability to your app.

The current implementation looks ok. However you can improve it by following this talk-https://www.youtube.com/watch?v=BlkJzgjzL0c

Upvotes: 2

Samuil Yanovski
Samuil Yanovski

Reputation: 2867

You've got the basics, so not much else to recommend. I'll just show you a few alternatives:

  • You can use ContentObserver and update the UI once there is new data in the database (no need to wait for a message from the service).
  • If you've got a lot of communication between Service <-> UI components, it might be easier if you take a look at Otto, EventBus or just restructure your code around Observables / RxJava.
  • You can move the Timeout logic to the service (it will be easier this way since all the error handling will be in a single place) and just return the error message to the UI. Most network frameworks allow you to set a Connection Timeout parameter and will fail the request after this time is reached. If you haven't looked at network frameworks yet - Retrofit + OkHttp is a great starting point.

Upvotes: 3

Related Questions