如何从我的 Android 应用中检测已安装的 Chrome 版本?

Jho*_*nty 7 android google-chrome

我正在开发一项功能,只有当设备上安装的 chrome 版本高于版本 65 时,我才需要将用户从 Android Native APP 转换为 Chrome 自定义选项卡。 所以我的问题是,有没有办法从 Android 检测 chrome 版本原生应用?

Mat*_*Pag 8

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;
}
Run Code Online (Sandbox Code Playgroud)

显然,这将一直有效,直到 Chrome 将像他们现在所做的那样进行版本控制,如果他们决定更改版本控制,逻辑也应该更新。(我不认为这会发生,但为了最大的安全性,把所有东西都放在一个 try-catch 中)