Abu*_*eji 4 android rx-android
我试图将更新发送到我Activity从我的GCMServiceListener话,我使用RxJava/RxAndroid并创建一个BusClass用于处理和发送Observers
public class ClientBus {
//private final PublishSubject<Object> _bus = PublishSubject.create();
// If multiple threads are going to emit events to this
// then it must be made thread-safe like this instead
private final Subject<Object, Object> _bus = new SerializedSubject<>(PublishSubject.create());
public void send(Object o) {
_bus.onNext(o);
}
public Observable<Object> toObserverable() {
return _bus;
}
public boolean hasObservers() {
return _bus.hasObservers();
}
}
Run Code Online (Sandbox Code Playgroud)
而在我,Application Class我这样做初始化BusClass
private ClientBus clientBus;
public ClientBus getRxBusSingleton() {
if (clientBus == null) {
clientBus = new ClientBus();
}
return clientBus;
}
Run Code Online (Sandbox Code Playgroud)
在我要接收消息的活动,我注册了CompositeSubscription,并得到一个参考,以我ClientBus class从Application Class
clientBus = ((MyApplication) getApplicationContext()).getRxBusSingleton();
@Override
protected void onStart() {
super.onStart();
initSubscriptions();
}
@Override
protected void onStop() {
super.onStop();
_subscriptions.unsubscribe();
}
void initSubscriptions() {
_subscriptions = new CompositeSubscription();
_subscriptions.add(clientBus.toObserverable().subscribe(new Action1<Object>() {
@Override
public void call(Object event) {
Log.e("New Event", "Event Received");
if (event instanceof MyGcmListenerService.Message) {
String msg = ((MyGcmListenerService.Message) event).getMessage();
if (msg.equals("Update Available")) {
scheduleArrayList = getSchedules();
scheduleAdapter = new ScheduleAdapter(getApplicationContext(), scheduleArrayList, ScheduledUberActivity.this);
scheduledList.setAdapter(scheduleAdapter);
scheduleAdapter.notifyDataSetChanged();
} else if (msg.equals("Refresh")) {
fetchTrips();
}
}
}
}));
}
Run Code Online (Sandbox Code Playgroud)
MyGcmListenerService class当我收到新通知时,我就这样做了
private void sendRefreshNotif() {
if (clientBus.hasObservers()) {<--It enters the if cause the Log prints. But, the activity doesn't get the message
Log.e("Obervers", "Observers aren't null");
clientBus.send(new Message("Refresh"));
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么不在这里工作?我用它在活动和片段之间进行交互.我关闭了我的应用程序以检查通知是否进入,它将进入此块,if (clientBus.hasObservers()) {但它没有启动应用程序并进行测试Observer,它注意到有一个活动的观察者.有帮助吗?谢谢.
小智 5
看起来你ClientBus在CompositeSubscription和中使用了不同的类实例MyApplication.尝试从ClientBus课堂上制作一个单身人士,它对我来说很好.
public class ClientBus {
public ClientBus(SingletonAccessor accessor) {}
private static ClientBus instance;
private static class SingletonAccessor{}
public static ClientBus getInstance() {
if (instance == null) instance = new ClientBus(new SingletonAccessor());
return instance;
}
private final Subject<Object, Object> mBus = new SerializedSubject<>(PublishSubject.create());
public void send(Object o) {
mBus.onNext(o);
}
public Observable<Object> toObserverable() {
return mBus;
}
public boolean hasObservers() {
return mBus.hasObservers();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
780 次 |
| 最近记录: |