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

Android 4.2上使用intent安装apk时可以启用降级吗?我发现有可能,当通过命令shell安装app时(使用-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);

解决方法

对于非平台(第三方)应用程序而言,这是不可能的:您必须直接向 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都是隐藏的,第三方应用无法访问.现在,您可以尝试反思,但我非常肯定平台会限制对它们的访问.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...