安装/卸载的APK编程(PackageManager VS意图) [英] install / uninstall APKs programmatically (PackageManager vs Intents)

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

问题描述

我的应用程序安装其他应用程序,它需要跟踪哪些应用程序已安装。当然,这可以通过简单地保持安装的应用程序的列表来实现。但是,这不应该是必要的!它应该是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:

公共抽象字符串 getInstallerPackageName (字符串的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 intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
startActivity(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仓库的Andr​​oid源$ C ​​$ C。下面是对应于意图的方法的两个隐藏方法。不幸的是,他们不提供给外部开发者。但是,也许他们会在未来的?

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

差异

  • 在使用意图本地包管理器没有意识到其应用程序的安装源于。具体来说,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.

      是否有可能使用意图指定包安装程序的名字? (可能是安装程序包的名称可以添加一个额外的安装意图是什么?)

      提示:如果您想下载的Andr​​oid源$ C ​​$ C,你可以按照这里介绍的步骤:下载源代码树。要提取* .java文件,并根据包的层次结构,你可以看看这个整洁的剧本把它们放在文件夹:的查看Android源$ C ​​$ C在Eclipse

      推荐答案

      这是不是目前提供给第三方应用程序。请注意,即使使用反射或其他招数来访问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方法的参数改变之一。)对于至少一个长时它被引入后,市场仍没有用它来跟踪它已经安装了的应用程序(和它很可能仍然不使用它),而只是用它来设置的Andr​​oid应用程序的反馈(这是独立于市场)作为主人照顾反馈。

      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 VS意图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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