以编程方式安装/卸载 APK(PackageManager 与 Intents) [英] install / uninstall APKs programmatically (PackageManager vs Intents)

查看:28
本文介绍了以编程方式安装/卸载 APK(PackageManager 与 Intents)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序安装了其他应用程序,它需要跟踪它安装了哪些应用程序.当然,这可以通过简单地保留已安装应用程序的列表来实现.但这应该不是必要的!PackageManager 应该负责维护 installedBy(a, b) 关系.事实上,根据API是:

My application installs other applications, and it needs to keep track of what applications it has installed. Of course, this could be achieved by simply keeping a list of installed applications. But this should not be necessary! It should be the responsibility of the PackageManager to maintain the installedBy(a, b) relationship. In fact, according to the API it is:

public abstract String getInstallerPackageName(String packageName) -检索安装包的应用程序的包名.这可以识别包裹来自哪个市场.

public abstract String getInstallerPackageName(String packageName) - Retrieve the package name of the application that installed a package. This identifies which market the package came from.

使用 Intent 安装 APK

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
startActivity(intent);

使用 Intent 卸载 APK:

Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package",
getPackageManager().getPackageArchiveInfo(apkUri.getPath(), 0).packageName,null));
startActivity(intent);

这显然不是方式,例如Android Market 安装/卸载软件包.他们使用更丰富的 PackageManager 版本.这可以通过从 Android Git 存储库下载 Android 源代码来查看.下面是对应于 Intent 方法的两个隐藏方法.不幸的是,它们不适用于外部开发人员.但也许他们会在未来?

This is obviously not the way e.g. Android Market installs / uninstalls packages. They use a richer version of the PackageManager. This can bee seen by downloading the Android source code from the Android Git repository. Below are the two hidden methods that corresponds to the Intent approach. Unfortunately they are not available to external developers. But perhaps they will be in the future?

使用 PackageManager 安装 APK

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

使用 PackageManager 卸载 APK

/**
 * Attempts to delete a package.  Since this may take a little while, the result will
 * be posted back to the given observer.  A deletion will fail if the calling context
 * lacks the {@link android.Manifest.permission#DELETE_PACKAGES} permission, if the
 * named package cannot be found, or if the named package is a "system package".
 * (TODO: include pointer to documentation on "system packages")
 *
 * @param packageName The name of the package to delete
 * @param observer An observer callback to get notified when the package deletion is
 * complete. {@link android.content.pm.IPackageDeleteObserver#packageDeleted(boolean)} will be
 * called when that happens.  observer may be null to indicate that no callback is desired.
 * @param flags - possible values: {@link #DONT_DELETE_DATA}
 *
 * @hide
 */
public abstract void deletePackage(
        String packageName, IPackageDeleteObserver observer, int flags);

差异

  • 使用 Intent 时,本地包管理器不知道安装源自哪个应用程序.具体来说,getInstallerPackageName(...) 返回 null.

    Differences

    • When using intents the local package manager is not made aware of which application the installation originated from. Specifically, getInstallerPackageName(...) returns null.

      隐藏方法 installPackage(...) 将安装程序包名称作为参数,并且很可能能够设置此值.

      The hidden method installPackage(...) takes the installer package name as a parameter, and is most likely capable of setting this value.

      是否可以使用意图指定包安装程序名称?(也许安装包的名称可以作为安装意图的额外内容?)

      提示:如果您想下载 Android 源代码,您可以按照此处描述的步骤操作:下载源代码树.要提取 *.java 文件并将它们根据包层次结构放在文件夹中,您可以查看这个简洁的脚本:在 Eclipse 中查看 Android 源代码.

      推荐答案

      这目前不适用于第三方应用程序.请注意,即使使用反射或其他技巧来访问 installPackage() 也无济于事,因为只有系统应用程序才能使用它.(这是因为是低级安装机制,权限被用户认可后,普通应用访问是不安全的.)

      This is not currently available to third party applications. Note that even using reflection or other tricks to access installPackage() will not help, because only system applications can use it. (This is because it is the low-level install mechanism, after the permissions have been approved by the user, so it is not safe for regular applications to have access to.)

      此外,installPackage() 函数参数在平台版本之间经常发生变化,因此您尝试访问它的任何操作都会在平台的各种其他版本上失败.

      Also the installPackage() function arguments have often changed between platform releases, so anything you do trying access it will fail on various other versions of the platform.

      另外值得指出的是,这个 installerPackage 是最近才添加到平台上的(2.2?),最初实际上并不用于跟踪谁安装了应用程序——平台使用它来确定何时启动谁报告应用程序的错误,以实现 Android 反馈.(这也是 API 方法参数更改的时间之一.)至少在引入后的很长一段时间内,Market 仍然没有使用它来跟踪它已安装的应用程序(它很可能仍然没有使用它)),而是使用它来将 Android 反馈应用(与电子市场分开)设置为所有者"来处理反馈.

      Also it is worth pointing out that this installerPackage was only added fairly recently to the platform (2.2?) and was originally not actually used for tracking who installed the app -- it is used by the platform to determine who to launch when reporting bugs with the app, for implementing Android Feedback. (This was also one of the times the API method arguments changed.) For at least a long while after it was introduced, Market still didn't use it to track the apps it has installed (and it may very well still not use it), but instead just used this to set the Android Feedback app (which was separate from Market) as the "owner" to take care of feedback.

      这篇关于以编程方式安装/卸载 APK(PackageManager 与 Intents)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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