gpt4 book ai didi

android - 以编程方式安装/卸载 APK(PackageManager 与 Intents)

转载 作者:IT老高 更新时间:2023-10-28 12:58:02 26 4
gpt4 key购买 nike

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

公共(public)抽象字符串 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(...) 将安装程序包名称作为参数,并且很可能能够设置此值。

问题

是否可以使用 Intent 指定包安装程序名称?(也许安装程序包的名称可以作为安装 Intent 的额外添加?)

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

最佳答案

Android P+ 在 AndroidManifest.xml 中需要此权限

<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />

然后:

Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:com.example.mypackage"));
startActivity(intent);

卸载。似乎更容易...

关于android - 以编程方式安装/卸载 APK(PackageManager 与 Intents),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6813322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com