I는 어떤 종래의 이벤트 발행에 의해 호출되는 이벤트 콜백 방법이벤트를 ReactiveX Observable에 넣는 방법?
public void onSomethingHappened(Information info) {
있다.
이제 이것을 ReactiveX로 랩핑하고자합니다.
어떻게 하시겠습니까?
내가 PublishSubject<T>
클래스를 발견하고 안드로이드 API에서 LocationListener
을 포장하기 위해 다음과 같은 방법이 사용
UPDATE :
public class LocationPublisher implements LocationListener {
@Override
public void onLocationChanged(Location location) {
LocationPublisher.onNext(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
StatusPublisher.onNext(new BundleEx(provider, status, extras));
}
@Override
public void onProviderEnabled(String provider) {
EnablenessPublisher.onNext(new Enableness(provider, true));
}
@Override
public void onProviderDisabled(String provider) {
EnablenessPublisher.onNext(new Enableness(provider, false));
}
public final PublishSubject<Location> LocationPublisher = PublishSubject.create();
public final PublishSubject<BundleEx> StatusPublisher = PublishSubject.create();
public final PublishSubject<Enableness> EnablenessPublisher = PublishSubject.create();
public static class Enableness {
private final String provider;
private final boolean enabled;
public Enableness(String provider, boolean enabled) {
this.provider = provider;
this.enabled = enabled;
}
public String getProvider() {
return provider;
}
public boolean isEnabled() {
return enabled;
}
}
public static class BundleEx {
private final String provider;
private final int status;
private final Bundle extras;
public BundleEx(String provider, int status, Bundle extras) {
this.provider = provider;
this.status = status;
this.extras = extras;
}
public String getProvider() {
return provider;
}
public int getStatus() {
return status;
}
public Bundle getExtras() {
return extras;
}
}
}
이 올바른 방법인가? 이제 LocationPublisher
, StatusPublisher
및 EnablenessPublisher
에 가입 할 수 있습니까?
기본적으로 Observable을 구현하는 모든 객체를 구독 할 수 있습니다. 그렇기 때문에 LocationPublisher 내의 모든 PublishSubjects 객체에 가입 할 수 있습니다. 좋은 접근이라면? 그것은 말하기 어렵습니다 ... 그것은 나를 위해 좋아 보인다. –
문제는 누구나 노출 된 주체에 값을 전달할 수 있으며 RxJava에는 r/o 주체가 없다는 것입니다. – Dims
r/o 제목은 무엇입니까 ?? –