首页 > 解决方案 > 如何在 oauth 成功时重定向到 Activity A 或在 oauth 失败时重定向到 Activity B

问题描述

我有三个活动 A、B 和 C。当在活动 C 中单击一个按钮时,它会打开一个浏览器意图,用户可以批准或拒绝该请求。当用户批准请求时,我想重定向回应用程序并打开活动 A,但如果用户拒绝请求,我也想重定向到应用程序但活动 B。

我已经成功地重定向回应用程序,但我不知道如何重定向到 A 或 B。

在此处的 api 文档中,它说当用户单击批准时,如果指定,他们将被重定向到自定义 url,但它没有说明用户是否拒绝请求。

这是我的代码

token = response.getString("request_token");

                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.themoviedb.org/authenticate/" + token + "?redirect_to=schemeName://hostName/path"));
                        startActivity(browserIntent);

清单.xml

<activity android:name=".LoginActivity" android:theme="@style/NoActionBar"
        android:configChanges="orientation" android:screenOrientation="portrait">
    <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:host="hostname"
            android:path="/path"
            android:scheme="schemename" />
    </intent-filter>
    </activity>

标签: android

解决方案


您可以启动结果的身份验证活动。这样,您可以将数据从第一个活动传递到第二个活动,并根据结果将它们返回到正确的活动。

在活动 C 中:

startActivityForResult(browserIntent, a_unique_integer_code);

在浏览器活动中:

    //if authentication succeeds
        Intent returnIntent = new Intent();       
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    //else:
        Intent returnIntent = new Intent(); 
        setResult(Activity.RESULT_CANCELED, returnIntent);
        finish();

并在您的活动 C 中接受结果并根据您获得的内容开始所需的活动:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == a_unique_integer_code) {
        if(resultCode == Activity.RESULT_OK){
            //start activity a
        }
        if (resultCode == Activity.RESULT_CANCELED) {
           //start activity b
        }
    }
}

推荐阅读