0

我已经在 android studio 中为一个网站实现了 chrome 自定义选项卡和 webview。他们都工作正常。现在我想要的是,如果用户没有安装 chrome 或者 chrome 版本小于 45(chrome 自定义选项卡所需的最低版本),那么打开 webview 类。如何检查 chrome 版本或是否安装了 chrome?这是默认打开 chrome 自定义选项卡的代码片段

4

3 回答 3

0

我出于自己的目的使用了以下代码来检查 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
}
于 2017-02-18T17:20:18.360 回答
0

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

于 2016-07-03T14:07:44.523 回答
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'是否为空

于 2016-07-04T17:40:00.500 回答