How to implement Activity-Service communication

I have the following situation:

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

  • When new data is available, it is loaded and stored in sqlite.
  • When the save to db is complete, the service passes the intent so that the activity knows in order to pull new data from the db.

User can request an immediate update ...

... in this case, I use Messenger to query the Services to search for new data.

Here is the problem:

the user is notified that the request continues, but it may take some time, it may be unsuccessful, it will never be able to return ...

I am currently receiving a message (using Messenger) back from the Service to Activity, informing the result of the request, or, if I do not receive the message, after x seconds I inform the user that the request was unsuccessful.

  • Please can you suggest a different approach?
  • I do not like to wait for a message, and if after x seconds no one is received, tell the user if there is a better way?
+7
java android
source share
3 answers

You have the basics, so not much to recommend. I will just show you some alternatives:

  • You can use ContentObserver and update the user interface after new data appears in the database (there is no need to wait for messages from the service).
  • If you have many connections between Service Service UI components, it would be easier if you had a look at Otto , EventBus, or just rebuild your code around Observables / RxJava .
  • You can move the Timeout logic to the service (this will be easier, since all error handling will be in one place) and simply return the error message to the user interface. Most network frameworks allow you to set the "Connection timeout" parameter and after this request the request will not be executed. If you havenโ€™t looked at the network framework, Retrofit + OkHttp is a great starting point.
+3
source share

You may consider optimistic rendering / optimistic updates . The template in which you update the user interface on the client, as if it were successful on the server. As soon as you receive a response from the server, you will update the user interface accordingly. You can send apps with new projects like google.

For more information, see the following discussions:

I assume that using this approach will provide better usability for your application.

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

+2
source share

If you think of it as a model manager problem, the problem here is the lack of a model to represent the state of the service. When the Service performs the update, this โ€œstateโ€ must be reflected in the user interface. Thus, the service needs to write this somewhere that the user interface 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 save this state in your database.

Another problem is notifying the user interface when this state changes. As mentioned in other posts, there are several ways to do this, such as LocalBroadcast, a message bus such as Otto, ContentObservers, etc.

+1
source share

All Articles