Reputation: 551
In my app I have used IntentService
and in it I call a webservice and parse my response. After my parsing is done I want to update the UI.
I saw that there is a way to update UI using broadcast receiver but is there any other way to update UI if I don't want to use broadcast receiver. If so then please share the link.
Upvotes: 2
Views: 1374
Reputation: 11487
You could either make a bound Service
, or use some lib like EventBus.
From the Android docs:
A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.
If you want to use this approach, you'll have to create a Service
that implements the onBind()
method. This method will return an IBinder
that you'll also have to implement. And that Binder
will use an interface
, that again, you'll have to create.
Example:
MyService.java
public class MyService extends Service {
// ...
@Override
public IBinder onBind(Intent intent) {
return new MyBinder(this);
}
}
MyBinder.java
public class MyBinder extends Binder {
private MyServiceInterface mService;
public MyBinder(MyServiceInterface s) {
mService = s;
}
public MyServiceInterface getService() {
return mService;
}
}
MyServiceInterface.java
public interface MyServiceInterface {
int someMethod();
boolean otherMethod();
Object yetAnotherMethod();
}
For more details, you can check the docs I've linked above.
Downside of this approach: Regular Service
class doesn't run on the background as IntentService
do. So you'll also have to implement a way of running things out of the main thread.
And from EventBus page:
- simplifies the communication between components
- decouples event senders and receivers
- performs well with Activities, Fragments, and background threads
- avoids complex and error-prone dependencies and life cycle issues
To use EventBus, the best way to start is following the documentation.
So, either of those approaches seems to be a good choice for you, just as using the BroadcastManager
(which I don't know why you can't use it).
Upvotes: 2