Reputation: 12905
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
Upvotes: 0
Views: 866
Reputation: 196
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 :)
Upvotes: 1