APK安装使用意图在Android上时启用降级4.2+ [英] Enable downgrade when installing apk using intent on Android 4.2+

查看:357
本文介绍了APK安装使用意图在Android上时启用降级4.2+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以安装APK时使用的意图在Android 4.2+启用降级?我发现,这是可能的,通过shell命令安装应用程序时(使用-d) ADB安装-r -d<链接APK> ,所以我希望它是在某种程度上可能也可以通过意向。我一直在寻找某种标志或东西,但我didin't找到任何有用的。

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

 意向意图=新的意图(Intent.ACTION_VIEW);
乌里applicatonFileUri = Uri.fromFile(applicationFile);
intent.setDataAndType(applicatonFileUri,PACKAGE_TYPE);
startActivity(意向);
 

解决方案

这是不可能的非平台(第三方)的应用程序:你必须让安装的请求的直接<一个href="https://developer.android.com/reference/android/content/pm/PackageManager.html">PackageManager.

PackageManager有非公开的API,<一个href="https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/content/pm/PackageManager.java">installPackage() (行2584截至记者发稿):

  / **
 * @隐藏
 *
 *安装包。因为这可能需要一小会儿,结果会
 *调回给定的观察者。安装会失败,如果调用上下文
 *缺少{@link android.Manifest.permission#INSTALL_PACKAGES}许可,如果
 已安装*在包文件清单命名的包,或者如果没有空间
 *可在设备上。
 *
 *参数packageURI包文件的位置来安装。这可以是一个文件:或
 *'的内容:URI。
 * @参数观测得到通知的观察员回调,包安装
 * 完成。 {@link IPackageInstallObserver#packageInstalled(字符串,INT)}会
 *调用时出现这种情况。观察者可以为空,表明没有回调的需要。
 *参数标志 - 可能值:{@link #INSTALL_FORWARD_LOCK},
 * {@link #INSTALL_REPLACE_EXISTING},{@link #INSTALL_ALLOW_TEST}。
 * @参数installerPackageName可选软件包正在执行的应用程序的名称
 * 安装。这标识从市场包装来了。
 * /
公共抽象无效installPackage(
        乌里packageURI,IPackageInstallObserver观测,INT标志,
        字符串installerPackageName);
 

,其中可能的标志之一是 INSTALL_ALLOW_DOWNGRADE

  / **
 *为{@link #installPackage}标志参数,以表明它是好的
 *安装更新到一个应用程序中新安装的应用程序,具有较低的
 *版本code比当前已安装的应用程序。
 *
 * @隐藏
 * /
公共静态最终诠释INSTALL_ALLOW_DOWNGRADE = 0x00000080;
 

所有这些API是隐藏的,而不是针对第三方应用程序进行访问。现在,你可以尝试反映,但我pretty的积极的平台,将限制对它们的访问反正。

Is possible to enable downgrade when installing apk using intent on Android 4.2+? I found out that it is possible, when installing app via command shell (using -d) adb install -r -d <link to apk>, so I hope it is somehow possible also via Intent. I was looking for some flag or something, but I didin't find anything helpful.

This is my intent to open package installer:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri applicatonFileUri = Uri.fromFile(applicationFile);
intent.setDataAndType(applicatonFileUri, PACKAGE_TYPE);
startActivity(intent);

解决方案

That's not possible for non-platform (3rd-party) apps: you have to make the installation request directly to PackageManager.

PackageManager has non-public API, installPackage() (line 2584 as of this writing):

/**
 * @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);

where one of the possible flags is 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;

All these APIs are hidden and not accessible for 3rd-party apps. Now, you may try reflection, but I am pretty positive that platform will restrict access to them anyway.

这篇关于APK安装使用意图在Android上时启用降级4.2+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆