简体   繁体   English

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

[英]Chrome Custom Tabs redirect to Android app will close the app

I am trying to implement an OAuth2 flow with an Android Chrome Custom Tab but my app is always closed (no crash) when the Chrome Custom Tab is receiving the 302 with the location/scheme of my app. 我正在尝试使用Android Chrome自定义标签实施OAuth2流程,但当Chrome自定义标签收到带有我的应用的位置/方案的302时,我的应用始终关闭(不会崩溃)。

If I create a HTML page with ahref link and touch manually on it the Chrome Custom Tab is correctly switching to my app. 如果我创建一个带有ahref链接并在其上手动触摸的HTML页面,Chrome自定义选项卡正在切换到我的应用程序。

Seems like when handling the server 302 redirect in the Chrome Custom Tab it will not correctly handle my custom app scheme... but why? 似乎在Chrome自定义选项卡中处理服务器302重定向时,它将无法正确处理我的自定义应用程序方案......但为什么呢?

If I try the same redirect URL in a stock browser or with a WebView everything is working too. 如果我在股票浏览器或WebView中尝试相同的重定向URL,一切都正常。

Here is my current setup: 这是我目前的设置:

MainActiviy.java 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);
        }
    });

AndroidManifest.xml 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>

This is the redirect URL that the app received with HTTP 302 code: 这是应用程序通过HTTP 302代码接收的重定向URL:

myappscheme://oauth?code=1234567&state=tokenCheck123 myappscheme:// OAuth的代码= 1234567&状态= tokenCheck123

build.gradle 的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+'
}

Thanks for any help... 谢谢你的帮助...

I've also observed my Android app unexpectedly background after server-side 302 redirection to a custom scheme, and observed expected handling from stand-alone Chrome and manually triggered redirection in the client. 在服务器端302重定向到自定义方案后,我还观察到我的Android应用程序意外背景,并观察到独立Chrome的预期处理和客户端中的手动触发重定向。

I was able to "fix" the issue by calling the warmup function before loading the url that redirects. 通过在加载重定向的URL之前调用warmup函数,我能够“修复”该问题。

In other words, this works: 换句话说,这有效:

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

This doesn't work: 这不起作用:

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

The Chrome Custom Tab docs describe warming up as a best practice, but it also appears to help ensure expected behavior. Chrome自定义标签文档将热身描述为最佳做法,但它似乎也有助于确保预期的行为。

In terms of env, I'm testing on a Nexus 5X w Chrome 51. My chrome tab dependency in Gradle looks like this: 就env而言,我正在使用Nexus 5X w Chrome 51进行测试。我在Gradle中的Chrome选项卡依赖关系如下所示:

dependencies {
    compile 'com.android.support:customtabs:24.0.0'

If I used android:launchMode="singleInstance" there were multiple instances in the task manager so this was no option. 如果我使用android:launchMode =“singleInstance”,那么任务管理器中有多个实例,所以这不是选项。

Starting the CustomTabsIntent with FLAG_ACTIVITY_NEW_TASK Flag did the trick. 使用FLAG_ACTIVITY_NEW_TASK Flag启动CustomTabsIntent可以解决问题。

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

intent.intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
intent.launchUrl(context, Uri.parse(url));

It helped me to set the Activity that I use to start a CustomTab to singleInstance mode in the manifest file: 它帮助我设置了用于在清单文件中将CustomTab启动到singleInstance模式的Activity:

    <activity
        android:launchMode="singleInstance"
        android:name="com.example.SingleInstanceActivityToStartCustomTab"
    </activity>

And in the code I do as usual: 在代码中我像往常一样:

    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.intent.setPackage(someChromePackage);
    customTabsIntent.launchUrl(singleInstanceModeActivity, someUriThatDoesRedirect);

I tried warming up Chrome and even calling customTabsIntent.launchUrl() with some delay after calling client.warmup(0l); 我试图热身Chrome和甚至称customTabsIntent.launchUrl()调用后,一些延迟client.warmup(0l); and neither helped. 并没有帮助。

I'm pretty sure this is the result of a bug in Chrome. 我很确定这是Chrome中的一个错误的结果。 I updated all of my devices (GS6, GS7, Nexus 7 and Nexus 9) to the latest version of Chrome and my app is no longer minimized when the redirect occurs. 我将所有设备(GS6,GS7,Nexus 7和Nexus 9)更新到最新版本的Chrome,当重定向发生时,我的应用程序不再被最小化。

I just discovered this today (11/3/2016) so I don't yet have any information about the specific bug or its subsequent resolution. 我今天刚刚发现这个(2016年3月3日),所以我还没有任何关于特定错误或其后续解决方案的信息。 This is just what I've noticed. 这正是我注意到的。

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android应用链接无法与Chrome自定义标签一起使用 - Android app links not working with Chrome Custom Tabs 从自定义标签重定向到应用 - redirect to app from custom tabs Android应用中的自定义标签 - Custom tabs in a Android App 在 Android 中从外部关闭 chrome 自定义选项卡 - Close chrome custom tabs from external in Android Android应用会自行关闭(带有标签和标签组的应用) - Android app close itself (app with tabs and tabgroup) 通过 Android 应用程序上的 Chrome 自定义选项卡下载文件时获得通知 - Get notified for file downloaded via Chrome custom tabs on on Android App 如果应用已获得授权,则Android Chrome自定义标签/ Fitbit网络API不会重定向。 (OAuth2.0的) - Android Chrome custom tabs / Fitbit web API won't redirect if app is already authorized. (OAuth2.0) Xamarin.Auth(Android) - Chrome自定义标签在重定向时不会关闭 - Xamarin.Auth (Android) - Chrome Custom Tabs Doesn't Close on Redirect 如何通过Chrome自定义标签中的intent / pendingIntent关闭应用/活动或代码菜单项 - How to close app/activity or code menu item via intent/pendingIntent in Chrome Custom Tabs 如何关闭 Chrome 自定义标签 - How to close chrome custom tabs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM