简体   繁体   English

URL 仍然使用 webView 打开,即使使用 android 自定义选项卡启动

[英]URL still opened with webView even tho launched with android custom tabs

I'm pretty new to Android development and building a simple demo app to try out the Android custom tabs functionality.我对 Android 开发和构建一个简单的演示应用程序来试用 Android 自定义选项卡功能非常陌生。 Yet somehow when I launched the app and tested it on the virtual device, I still see the URL is launched with webView instead of custom tabs.然而不知何故,当我启动该应用程序并在虚拟设备上对其进行测试时,我仍然看到 URL 是使用 webView 而不是自定义选项卡启动的。 Am I missing something?我错过了什么吗? Thanks so much!非常感谢!

*the reason I'm trying is to bypass the facebook/Google login restriction nowadays on webview *我正在尝试的原因是绕过现在 webview 上的 facebook/Google 登录限制

在此处输入图像描述

my build.gradle :我的build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.example.helloworld"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation "androidx.browser:browser:1.3.0"
}

My MainActivity.java:我的MainActivity.java:

package com.example.helloworld;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.browser.customtabs.CustomTabsIntent;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button loginBtn = findViewById(R.id.login);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String loginUrl = "https://google.com";

                CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
                CustomTabsIntent customTabsIntent = builder.build();
                customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                customTabsIntent.launchUrl(getApplicationContext(), Uri.parse(loginUrl));
            }
        });

    }
}

Use PackageManager to check the device' s browser in code.使用PackageManager在代码中检查设备的浏览器。 https://developer.chrome.com/docs/android/custom-tabs/integration-guide/ https://developer.chrome.com/docs/android/custom-tabs/integration-guide/

User needs to have a browser app installed.用户需要安装浏览器应用程序。 Most browsers these days do support CustomTabs.现在大多数浏览器都支持 CustomTabs。

If there is not browser installed or the installed browser does not support CustomTabs, CustomTabs opens the URL in WebView.如果没有安装浏览器或者安装的浏览器不支持CustomTabs,CustomTabs会打开WebView中的URL。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM