相关疑难解决方法(0)

以编程方式安装/卸载APK(PackageManager vs Intents)

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

public abstract String getInstallerPackageName(String packageName) - 检索安装包的应用程序的包名称.这确定了包裹来自哪个市场.

目前的做法

使用Intent安装APK

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

使用Intent卸载APK:

Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package",
getPackageManager().getPackageArchiveInfo(apkUri.getPath(), 0).packageName,null));
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

这显然不是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 …
Run Code Online (Sandbox Code Playgroud)

android install uninstall android-intent apk

137
推荐指数
4
解决办法
13万
查看次数

标签 统计

android ×1

android-intent ×1

apk ×1

install ×1

uninstall ×1