0

I have implemented both chrome custom tab and webview for a website in android studio. They are both working fine. Now what I want is that if the user doesn't have chrome installed or has a chrome version less than 45 (minimum version needed for chrome custom tab), then open webview class. How to check the chrome version or whether chrome is installed or not? Here is the code snipet to open chrome custom tab by default

mdrafiqulrabin
  • 1,657
  • 5
  • 28
  • 38
Yash Sharan
  • 51
  • 2
  • 7

3 Answers3

0

You should try to bind to the service and if it fails then you can fallback to the webview. You can see it here: https://github.com/GoogleChrome/custom-tabs-client/blob/cc6b8b9169ed7e70484bbdbbf39b672d1c4b3c80/Application/src/main/java/org/chromium/customtabsclient/MainActivity.java#L147

ade
  • 4,056
  • 1
  • 20
  • 25
0

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

you can check if 'packageName' is null

gutte
  • 1,373
  • 2
  • 19
  • 31
0

I have used the following code for my own purpose to check the chrome version with whether chrome is installed or not. Hope it will help, you should try.

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
}
mdrafiqulrabin
  • 1,657
  • 5
  • 28
  • 38