Thank you :) Those who are asking about COPA_RESULT, it's nothing but a user generated static variable. "COPA" is his service name so you can replace it with yours completely. In my case, it's static final public String MP_Result = "com.widefide.musicplayer.MusicService.REQUEST_PROCESSED";
– TheOnlyAnilSep 12 '15 at 19:21

1

UI won't update if the user navigates away from the Activity and then comes back to it after the service has finished. What is the best way to handle that?
– SavageKingApr 28 '16 at 19:46

1

@SavageKing You need to send whatever request to the service is appropriate in your onStart() or onResume() method. In general if the Activity asks the Service to do something, but quits before receiving the result, it's reasonable to assume the result is no longer required. Similarly on starting an Activity it should assume that there are no outstanding requests being processed by the Service.
– ClydeApr 29 '16 at 20:36

I would use a bound service to do that and communicate with it by implementing a listener in my activity. So if your app implements myServiceListener, you can register it as a listener in your service after you have bound with it, call listener.onUpdateUI from your bound service and update your UI in there!

+1 This is a viable solution, but on my case, I really need the service to keep running even though all of the activity unbinds to it (e.g. User close the application, Premature OS termination), I have no choice but to use broadcast receivers.
– Neon WargeJan 3 '17 at 7:48

Clyde's solution works, but it is a broadcast, which I am pretty sure will be less efficient than calling a method directly. I could be mistaken, but I think the broadcasts are meant more for inter-application communication.

I'm assuming you already know how to bind a service with an Activity.
I do something sort of like the code below to handle this kind of problem:

LocalBroadcastManager is designed for communication within an application, so is quite efficient. The bound service approach is ok when you have a limited number of clients for the service and the service doesn't need to run independently. The local broadcast approach allows the service to be more effectively decoupled from its clients, and makes thread-safety a non-issue.
– ClydeJan 22 '14 at 21:59

My solution might not be the cleanest but it should work with no problems.
The logic is simply to create a static variable to store your data on the Service and update your view each second on your Activity.

Let's say that you have a String on your Service that you want to send it to a TextView on your Activity. It should look like this

Your Service:

public class TestService extends Service {
public static String myString = "";
// Do some stuff with myString

@MurtazaKhursheedHussain - can you elaborate more on that?
– SolidSnakeNov 20 '17 at 8:44

2

Static members are the source of memory leaks in activities ( there are a lot of articles around) plus keeping them in service makes it worse. A broadcast receiver is much more appropriate in OP's situation or keeping in persistent storage is also a solution.
– Murtaza Khursheed HussainNov 20 '17 at 10:07

@MurtazaKhursheedHussain - so let's say if I have a class (not a service class just some model class I use for populating data) with a static Hashmap that gets re-populated with some data (coming from an API) every minute is considered as a memory leak?
– SolidSnakeNov 21 '17 at 4:43

In the context of android yes, if its a list, try to create an adapter or persist data using sqllite/realm db.
– Murtaza Khursheed HussainNov 21 '17 at 7:02