我的应用程序安装了其他应用程序,它需要跟踪已安装的应用程序。当然,这可以通过保留已安装应用程序的列表来实现。但这没有必要!PackageManager 应该负责维护 installedBy (a,b)关系。事实上,根据空气污染指数,它是:
公共抽象字符串 GetInstallerPackageName(字符串 packageName)- 检索安装软件包的应用程序的软件包名称。这标识软件包来自哪个市场。
使用意图安装 APK
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 存储库下载 Android 源代码可以看到这一点。下面是与意图方法相对应的两个隐藏方法。不幸的是,外部开发人员无法使用它们。但也许将来会是这样呢?
使用 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。
隐藏方法 installPackage (...)将安装程序包名称作为参数,并且很可能能够设置此值。
是否可以使用意图指定包安装程序名称? (也许安装程序包的名称可以作为安装意图的附加内容?)
提示: 如果你想下载 Android 的源代码,你可以按照这里描述的步骤: 下载源代码树。提取 * 。Java 文件,并把他们放在文件夹根据包的层次结构,你可以检查出这个整洁的脚本: 在 Eclipse 中查看 Android 源代码。