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

2
  • Um, RxJava 2 is a library. Beyond that, when I search Google for rxjava2 listener, I come up with things like this and this and this and many others. Commented Jan 2, 2018 at 15:21
  • Ok, I meant, no libraries besides RxJava 2 :) I saw a lib for LocationListener. And thank you for links Commented Jan 2, 2018 at 15:28

1 Answer 1

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 :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.