在Android 4.2+上使用intent安装apk时启用降级

问题描述 投票:7回答:2

在Android 4.2+上使用intent安装apk时是否可以启用降级?我发现通过命令shell(使用-d)adb install -r -d <link to apk>安装应用程序是可能的,所以我希望它也可以通过Intent以某种方式实现。我正在寻找一些旗帜或东西,但我没有找到任何有用的东西。

这是我打开包安装程序的意图:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri applicatonFileUri = Uri.fromFile(applicationFile);
intent.setDataAndType(applicatonFileUri, PACKAGE_TYPE);
startActivity(intent);
android android-intent apk android-package-managers
2个回答
12
投票

对于非平台(第三方)应用程序,这是不可能的:您必须直接向PackageManager发出安装请求。

PackageManager具有非公共API,installPackage()(撰写本文时的第2584行):

/**
 * @hide
 *
 * Install a package. Since this may take a little while, the result will
 * be posted back to the given observer.  An installation will fail if the calling context
 * lacks the {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if the
 * package named in the package file's manifest is already installed, or if there's no space
 * available on the device.
 *
 * @param packageURI The location of the package file to install.  This can be a 'file:' or a
 * 'content:' URI.
 * @param observer An observer callback to get notified when the package installation is
 * complete. {@link IPackageInstallObserver#packageInstalled(String, int)} will be
 * called when that happens.  observer may be null to indicate that no callback is desired.
 * @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
 * {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
 * @param installerPackageName Optional package name of the application that is performing the
 * installation. This identifies which market the package came from.
 */
public abstract void installPackage(
        Uri packageURI, IPackageInstallObserver observer, int flags,
        String installerPackageName);

其中一个可能的标志是INSTALL_ALLOW_DOWNGRADE

/**
 * Flag parameter for {@link #installPackage} to indicate that it is okay
 * to install an update to an app where the newly installed app has a lower
 * version code than the currently installed app.
 *
 * @hide
 */
public static final int INSTALL_ALLOW_DOWNGRADE = 0x00000080;

所有这些API都是隐藏的,第三方应用无法访问。现在,您可以尝试反思,但我非常肯定平台会限制对它们的访问。


0
投票

另一种可能的解决方案是让另一个应用程序首先卸载您的应用程序,然后再次安装它。我找不到另一种方式,如果有人找到更好的解决方案,请让我知道:)

© www.soinside.com 2019 - 2024. All rights reserved.