Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect installed Chrome version from my Android App?

I am working on a feature where I need to transition user from Android Native APP to Chrome custom tab only if chrome version installed on the device is greater than version 65. So my question is, is there a way we can detect chrome version from Android Native App ?

like image 818
Jhonty Avatar asked Aug 07 '18 18:08

Jhonty


1 Answers

private boolean isChromeInstalledAndVersionGreaterThan65() {
    PackageInfo pInfo;
    try {
        pInfo = getPackageManager().getPackageInfo("com.android.chrome", 0);
    } catch (PackageManager.NameNotFoundException e) {
        //chrome is not installed on the device
        return false;
    }
    if (pInfo != null) {
        //Chrome has versions like 68.0.3440.91, we need to find the major version
        //using the first dot we find in the string
        int firstDotIndex = pInfo.versionName.indexOf(".");
        //take only the number before the first dot excluding the dot itself
        String majorVersion = pInfo.versionName.substring(0, firstDotIndex);
        return Integer.parseInt(majorVersion) > 65;
    }
    return false;
}

Obviously this will work until Chrome will be versioned how they did until now, if they will decide to change the versioning the logic should be updated as well. (I don't think this will happen but for max safety put everything in a try-catch)

like image 119
MatPag Avatar answered Oct 02 '22 19:10

MatPag