【发布时间】:2011-10-12 09:39:10
【问题描述】:
我的应用程序安装了其他应用程序,它需要跟踪它安装了哪些应用程序。当然,这可以通过简单地保存已安装应用程序的列表来实现。但这不应该是必要的!维护 installedBy(a, b) 关系应该是 PackageManager 的责任。实际上,根据 API 是:
公共抽象字符串 getInstallerPackageName(String packageName) - 检索安装包的应用程序的包名。这可以确定包裹来自哪个市场。
目前的做法
使用 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 方法相对应的两个隐藏方法。不幸的是,外部开发人员无法使用它们。但也许他们会在未来?
更好的方法
使用 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。
隐藏方法 installPackage(...) 将安装程序包名称作为参数,并且很可能能够设置此值。
问题
是否可以使用意图指定包安装程序名称? (也许安装程序包的名称可以作为安装意图的额外添加?)
提示:如果您想下载 Android 源代码,您可以按照此处描述的步骤操作:下载源代码树。要提取 *.java 文件并根据包层次结构将它们放在文件夹中,您可以查看这个简洁的脚本:View Android Source Code in Eclipse。
【问题讨论】:
-
文本中缺少某些 URI。我会尽快添加它们(新用户有一些限制以防止垃圾邮件)。
-
如何禁用卸载功能?
-
@user938893:“如何禁用卸载功能?” -- 我们正在处理一些难以卸载的恶意软件,是吗?
标签: android android-intent installation uninstallation apk