0

I try to understand if it's possible to handle android listener events with RxJava 2. I guess, it is, but just couldn't find an answer.

In my case I would like to handle speech recognition listener events.

I only want to use RxJava 2 without any libraries.

Thank you in adavance

Tima
  • 12,765
  • 23
  • 82
  • 125
  • Um, RxJava 2 *is* a library. Beyond that, when I search Google for `rxjava2 listener`, I come up with things like [this](https://stackoverflow.com/q/42118782/115145) and [this](https://stackoverflow.com/q/44833118/115145) and [this](https://stackoverflow.com/q/46332016/115145) and many others. – CommonsWare Jan 02 '18 at 15:21
  • Ok, I meant, no libraries besides RxJava 2 :) I saw a lib for LocationListener. And thank you for links – Tima Jan 02 '18 at 15:28

1 Answers1

1

I don't know exactly what you want to achieve, but this seems like a perfect case for Subjects. For more details check out the link, but in one word it's an entity that you can push your changes to and treat it as an observable. You can push every object that you get, while you are listening for changes, and everyone that is subscribed to the subject will get this data.

Simple usage for it looks like this:

First you initialize the Subject. I will use PublishSubject, but you can use whatever suites your needs (check out the link I have provided).

Subject<MyObject> subject = PublishSubject.create();

Then you register you listener, and when you get a new object you simply push it to the subject.

something.registerListener( result -> subject.onNext(result));

Then you expose this subject to the entity that wants to perform operations like this:

Observable<MyObject> observeChanges(){
    return subject;
}

And then you use it as an observable. Voilà

Hope it helps :)

Kamil Dziadek
  • 196
  • 1
  • 5