Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement communication Activity-Service

Tags:

java

android

I have the following situation:

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

  • when new data are available they are downloaded and saved on sqlite.
  • when the save to db is complete the service broadcasts an intent so that the activity knows to pull the new data from the db.

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?
like image 380
Lisa Anne Avatar asked Dec 02 '15 19:12

Lisa Anne


People also ask

How do you communicate with service and activity?

We already know that we can communicate with Service from activity just by using method startService() and passing Intent to the argument in the method, or either we can use bindService() to bind the service to the activity with argument Intent.

How do I start a service from activity?

You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.

What is bindService in android?

A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC).

What is the difference between activity and service?

Services are a unique component in Android that allows an application to run in the background to execute long-running operation activities, on the other hand, an activity, like a window or a frame in Java, represents a single screen with a user interface.


1 Answers

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.
like image 52
Samuil Yanovski Avatar answered Oct 21 '22 01:10

Samuil Yanovski