在多个活动中处理oauth意图

时间:2018-06-22 13:45:40

标签: android oauth-2.0

我需要在多个活动中处理oauth意向响应,但仅在当前活动中进行处理。

为什么我需要这个?我有一个登录名,当用户记录所有内容后,他会确认ouath,然后将其重定向到主要活动,但是ouath令牌过期后,我需要提示用户重新认证并处理此活动的意图。

当我将以下标记添加到多个活动中时。

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="..." />
</intent-filter>

我选择要处理oauth结果的那个,这不是我所需要的,我需要主动活动来自动接收结果。

2 个答案:

答案 0 :(得分:0)

您可以使用RxBus实现。 首先,您必须创建RxBus类。

public class RxBus {
private static RxBus instance;
private PublishSubject<Event> subject = PublishSubject.create();

public static synchronized RxBus getInstance() {
    if (instance == null) {
        instance = new RxBus();
    }

    return instance;
}

private RxBus(){}

public void postEvent(Event event){
    subject.onNext(event);
}

public Observable<Event> getEvents(){
    return subject;
}

}

现在,您应该在BaseActivity或类似的东西(取决于您或您的项目结构)中订阅它。

private RxBus rxbus;
private Subscription rxBusSubscription;

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rxBus = RxBus.getInstance();
    }

@Override
protected void onResume() {
    super.onResume();

    if (shouldSubscribeRxBus()) {
        rxBusSubscription = rxBus.getEvents()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(event -> {
                    if (event.getEventType() == Event.EventType.LOGOUT_USER) {
                        showSessionExpiredDialog();
                    }
                });
    }
}

现在,您可以根据需要实现showSessionExpiredDialog。

当您接听来电时

RxBus.getInstance().postEvent(new LogoutUserEvent());

您可以根据需要创建事件类结构。

答案 1 :(得分:0)

我最终要做的是:我创建了一个处理意图的活动,然后广播对其他活动的响应。

在其他活动上,我在onStart / onStop回调中添加和删除了广播侦听器。

我尝试使用onResume方法添加侦听器,但是由于正在广播的活动仍然处于前台,因此我无法捕获传入的广播。

相关问题