gpt4 book ai didi

android - 配置 RxJava 以将数据从 GCMListenerService 发送到 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:00 25 4
gpt4 key购买 nike

我正在尝试从我的 GCMServiceListener 向我的 Activity 发送更新,所以,我正在使用 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();
}
}

在我的 Application Class 中,我这样做是为了初始化 BusClass

private ClientBus clientBus;

public ClientBus getRxBusSingleton() {
if (clientBus == null) {
clientBus = new ClientBus();
}
return clientBus;
}

在我想接收消息的 Activity 中,我注册了一个 CompositeSubscription 并从 Application Class 获取了对我的 ClientBus 类 的引用

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();
}
}
}
}));
}

MyGcmListenerService 类中,我在收到新通知时执行了此操作

 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"));
}
}

我不明白的是为什么它在这里不起作用?我用它在 Activity 和 fragment 之间进行交互。我关闭了我的应用程序以检查是否收到通知,它将进入此 block if (clientBus.hasObservers()) { 但它没有并启动应用程序并测试 Observer ,它注意到有一个活跃的观察者。有什么帮助吗?谢谢。

最佳答案

您似乎在 CompositeSubscriptionMyApplication 中使用了 ClientBus 类的不同实例。尝试从 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();
}
}

关于android - 配置 RxJava 以将数据从 GCMListenerService 发送到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35943237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com