18

I have a Bound Service which responsible for downloading files and thus it knows the downloading status/progress. And the UI (Fragment or Activity) has to show/update download progress from the service.

Actually i think the common way is to use BroadcastReciever or a CallBack from Activity. But i heard somewhere about using RxJava (ReactiveX Programming) and mediator class (and Dagger to inject it into both service and activity) which is mentioned below.

So my question is how to handle RxJava with these bunch of stuff? Any Code Samples? Is there another efficient way than using intents?

Resource: More efficient way of updating UI from Service than intents? [ see the first answer update ]

6
  • 2
    Personally I prefer using Messenger service: developer.android.com/reference/android/os/Messenger.html
    – mjp66
    Commented Nov 20, 2015 at 21:24
  • 1
    I thought I had an example of using Messenger somewhere here, I guess not, but here's a good post detailing its use: stackoverflow.com/questions/4300291/…
    – mjp66
    Commented Nov 20, 2015 at 21:27
  • just use "local bound service" pattern, no need for any external libs and it is the fastest solution
    – pskink
    Commented Nov 20, 2015 at 22:08
  • 2
    yes, this is exactly what i mean, but if you store your downloads in some sqlite db you could use a CursorLoader that queries your custom ContentProvider and the service updates/inserts/deletes your provider, in this case there is no need for any callbacks
    – pskink
    Commented Nov 20, 2015 at 22:51
  • 3
    @AmirHoseinKazemNejad It's not exactly what you are looking for but maybe this article will inspire you donnfelker.com/rxjava-with-aidl-services Commented Nov 21, 2015 at 14:35

2 Answers 2

15

Required of RxJava/RxAndroid

1) OBSERVABLES 2) OBSERVERS 3) SUBSCRIBE

Plan where you need to place your 1, 2, 3,

OBSERVABLES go where the data is created, so In your case SERVICE

OBSERVERS go where the data needs to be consumed(or displayed), so that's your ACTIVITY

SUBSCRIBE goes anywhere where you have access to OBSERVABLE & OBSERVER, so lets use ACVITITY for that

Procedure and Code:

Firstly, Prepare your OBSERVABLE in service like this

class MyService extends Service {
    static PublishSubject<String> data = PublishSubject.create();

   @Override
   public void onStarCommand(Intent intent,int i, int j){
        # DO THIS ANYWHER WHERE YOU GENERATE DATA 
        data.onNext("Hello");
   }

   public static Observable<String> getObservable(){
      return data;
   }
}

Secondly, Prepare your OBSERVER(and OBSERVABLE) in Activity's onCreate

Observable<String> observable = MyService.getObservable();
Observer<String> observer = new Observer<String>() {
        @Override
        public void onCompleted() {
            Log.d(TAG, "onCompleted: ");
        }

        @Override
        public void onError(Throwable e) {
            Log.e(TAG, "onError: ",e);
        }

        @Override
        public void onNext(String text) {
            Log.d(TAG, "DATA reveived here: "+text);
        }
    };

Lastly Link both OBSERVER and OBSERVABLE in Activity, else Observable will not respond, use this again in onCreate

observable.subscribe(observer);

DONE, Now when even the data is triggered from Service using onNext(), the data arrives in Activity

3
  • 1
    And how to do this without static fields?
    – Mikel
    Commented Mar 5, 2019 at 16:09
  • 1
    public void onCompleted() should be public void onComplete(). Also missing onSubscribe() method. Commented Mar 10, 2019 at 19:16
  • app crashes when activity/fragment is destroyed or not active.. how to handle this? Commented Aug 13, 2020 at 10:52
4

The Binder subclass you use when something binds to your Service can expose a method that returns an Observable which emits progress data.

The caveat with this approach is that you have two resources which you need to release when the Activity instance is no longer valid: the service connection and the observable subscription.

5
  • 1
    can you explain your comment , or give some example ? Commented Nov 17, 2016 at 20:48
  • 3
    Bad answer. Observable is not serialized object, so not sure if it will work, especially if service is designed for cross-process interaction. Wont work.
    – msangel
    Commented Jan 30, 2017 at 16:13
  • 1
    Cross-process interaction is outside the scope of this question. Otherwise, it's basically the same as exposing your Service instance via the Binder, and the Service is also not serializable.
    – npace
    Commented Feb 1, 2017 at 7:39
  • 1
    @LeonardoAcevedo, haven't tested, but shouldn't work
    – msangel
    Commented Feb 20, 2017 at 2:58
  • @msangel for a local bound service there should be no need for serialization, do you think that would work on that case? Commented Feb 20, 2017 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.