简体   繁体   中英

How to Upgrade APK with Install Intent

I need to upgrade an APK that is distributed outside of Play Store. I was able to download the new APK and launched the install with this intent

    private void startInstall(File appApkFile) {
    if (appApkFile.exists()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", appApkFile);
            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            intent.setData(apkUri);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");

            PackageManager packageManager = getPackageManager();
            if (intent.resolveActivity(packageManager) != null) {
                startActivity(intent);
            }
        } else {
            Uri apkUri = Uri.fromFile(appApkFile);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            PackageManager packageManager = getPackageManager();
            if (intent.resolveActivity(packageManager) != null) {
                startActivity(intent);
            }
        }
    } 
}

The install launches however because the app already exists on the device the install fails with the error message that "the package conflicts with an existing package by the same name"

Would anyone know how to pass the upgrade flag to the Install Intent action?

APK安装失败

请在调试版和发行版中使用签名的apk文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM