【问题标题】:How to switch between chrome custom tab and webview in android?如何在android中的chrome自定义标签和webview之间切换?
【发布时间】:2016-07-03 08:54:27
【问题描述】:

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

【问题讨论】:

    标签: android webview chrome-custom-tabs


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      取自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'是否为空

      【讨论】:

        【解决方案3】:

        我出于自己的目的使用了以下代码来检查 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
        }
        

        【讨论】:

          猜你喜欢
          • 2016-12-21
          • 2017-02-27
          • 1970-01-01
          • 2012-11-07
          • 2017-02-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-05
          • 2014-09-26
          相关资源
          最近更新 更多