我的应用程序会安装其他应用程序,它需要跟踪安装了哪些应用程序.当然,这可以通过简单地保存已安装应用程序的列表来实现.但这不应该是必要的!包装经理应负责维护installedBy(a,b)关系.事实上,根据API,它是:
公共抽象字符串getInstallerPackageName(字符串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源代码来看到.下面是与意图方法相对应的两个隐藏方法.不幸的是,它们对外部开发人员不可用.但也许他们会在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);
差异
使用Intents时,本地包管理器不知道安装来自哪个应用程序.具体地说,getInsteller PackageName(.)返回NULL.
隐藏方法installPackage(…)将安装程序包名称作为参数,并且很可能能够设置此值.
问题
是否可以使用intents指定软件包安装程序名称
提示:如果你想下载Android源代码,你可以按照这里描述的步骤:下载源代码树.提取*.java文件,并根据包的层次 struct 将它们放在文件夹中,您可以查看以下整洁的脚本:View Android Source Code in Eclipse我>