如何在android中切换chrome自定义选项卡和webview?

时间:2016-07-03 08:54:27

标签: android webview chrome-custom-tabs

我已经在android studio中为网站实现了chrome自定义选项卡和webview。他们都工作正常。现在我想要的是,如果用户没有安装chrome或者chrome版本低于45(chrome自定义选项卡需要最低版本),那么打开webview类。如何检查chrome版本或是否安装了chrome? Here is the code snipet to open chrome custom tab by default

3 个答案:

答案 0 :(得分:0)

您应该尝试绑定到该服务,如果它失败,那么您可以回退到webview。你可以在这里看到它:https://github.com/GoogleChrome/custom-tabs-client/blob/cc6b8b9169ed7e70484bbdbbf39b672d1c4b3c80/Application/src/main/java/org/chromium/customtabsclient/MainActivity.java#L147

答案 1 :(得分:0)

取自https://github.com/GoogleChrome/custom-tabs-client/blob/master/demos/src/main/java/org/chromium/customtabsdemos/CustomTabActivityHelper.java

public void bindCustomTabsService(Activity activity) {
    if (mClient != null) return;

    String packageName = CustomTabsHelper.getPackageNameToUse(activity);
    if (packageName == null) return;

    mConnection = new ServiceConnection(this);
    CustomTabsClient.bindCustomTabsService(activity, packageName, mConnection);
}

您可以查看' packageName'为空

答案 2 :(得分:0)

为了我自己的目的,我使用了以下代码来检查chrome版本是否安装了chrome。希望它会有所帮助,你应该尝试。

String chromePackageName = "com.android.chrome";
int chromeTargetVersion  = 45;

boolean isSupportCustomTab = false;

try {
    PackageManager pm = getApplicationContext().getPackageManager();
    List<PackageInfo> list = pm.getInstalledPackages(PackageManager.MATCH_DEFAULT_ONLY);
    if (list != null && 0 < list.size()) {
        for (PackageInfo info : list) {
            if (chromePackageName.equals(info.packageName)) {

                String chromeVersion = pm.getPackageInfo(chromePackageName, 0).versionName;
                if(chromeVersion.contains(".")) {
                    chromeVersion = chromeVersion.substring(0, chromeVersion.indexOf('.'));
                }
                isSupportCustomTab = (Integer.valueOf(chromeVersion) >= chromeTargetVersion);

                break;
            }
        }
    }
} catch (Exception ex) {}

if (isSupportCustomTab) {
    //Use Chrome Custom Tab
} else {
    //Use WebView
}