如何使用安装意图升级APK

时间:2018-10-17 03:46:49

标签: android apk

我需要升级在Play商店之外分发的APK。我能够下载新的APK并以此意图启动安装

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

但是安装会启动,因为该应用程序已经存在于设备上,所以安装失败,并显示错误消息:“程序包与同名的现有程序包冲突”

有人会知道如何将升级标志传递给Install Intent操作吗?

APK Installed Failed

1 个答案:

答案 0 :(得分:1)

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