Chrome自定义标签重定向到Android应用将关闭该应用

sys*_*ast 13 android google-chrome oauth chrome-custom-tabs

我正在尝试使用Android Chrome自定义标签实施OAuth2流程,但当Chrome自定义标签收到带有我的应用的位置/方案的302时,我的应用始终关闭(不会崩溃).

如果我创建一个带有ahref链接并在其上手动触摸的HTML页面,Chrome自定义选项卡正在切换到我的应用程序.

似乎在Chrome自定义选项卡中处理服务器302重定向时,它将无法正确处理我的自定义应用程序方案......但为什么呢?

如果我在股票浏览器或WebView中尝试相同的重定向URL,一切都正常.

这是我目前的设置:

MainActiviy.java

    Button btnChromeCustomTab = (Button) findViewById(R.id.btnChromeCustomTab);
    btnChromeCustomTab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
            String packageName = CustomTabsHelper.getPackageNameToUse(MainActivity.this);
            customTabsIntent.intent.setPackage(packageName);
            Uri theLocationUri = Uri.parse(URL);
            customTabsIntent.launchUrl(MainActivity.this, theLocationUri);
        }
    });
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter android:label="@string/filter_title">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="myappscheme" android:host="oauth" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

这是应用程序通过HTTP 302代码接收的重定向URL:

myappscheme:// OAuth的代码= 1234567&状态= tokenCheck123

的build.gradle

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "de.myapptest.webviewtest"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:23.2.1'
   compile 'com.android.support:design:23.2.1'
   compile 'com.android.support:customtabs:23.0.0+'
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助...

小智 7

在服务器端302重定向到自定义方案后,我还观察到我的Android应用程序意外背景,并观察到独立Chrome的预期处理和客户端中的手动触发重定向.

通过在加载重定向的URL之前调用warmup函数,我能够"修复"该问题.

换句话说,这有效:

void launchTab(Context context, Uri uri){
    final CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
        @Override
        public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient client) {
            final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            final CustomTabsIntent intent = builder.build();
            client.warmup(0L); // This prevents backgrounding after redirection
            intent.launchUrl(context, uri);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {}
    };
    CustomTabsClient.bindCustomTabsService(context, "com.android.chrome", connection);
}
Run Code Online (Sandbox Code Playgroud)

这不起作用:

void launchTab(Context context, Uri uri){
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final CustomTabsIntent intent = builder.build();
    intent.launchUrl(context, uri);
}
Run Code Online (Sandbox Code Playgroud)

浏览器自定义选项卡文档描述了升温的最佳实践,但它似乎还有助于确保预期的行为.

就env而言,我正在使用Nexus 5X w Chrome 51进行测试.我在Gradle中的Chrome选项卡依赖关系如下所示:

dependencies {
    compile 'com.android.support:customtabs:24.0.0'
Run Code Online (Sandbox Code Playgroud)

  • 我已经有了client.warmup(0L),我的应用程序仍然做同样的事情.还有其他想法吗? (2认同)

Seb*_*ian 6

如果我使用 android:launchMode="singleInstance" 任务管理器中有多个实例,所以这是没有选择的。

使用 FLAG_ACTIVITY_NEW_TASK 标志启动 CustomTabsIntent 就可以了。

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent intent = builder.build();

intent.intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
intent.launchUrl(context, Uri.parse(url));
Run Code Online (Sandbox Code Playgroud)