android – 以编程方式安装/卸载APK(PackageManager vs Intents)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android – 以编程方式安装/卸载APK(PackageManager vs Intents),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4201字,纯文字阅读大概需要7分钟。
内容图文
我的应用程序安装其他应用程序,它需要跟踪它已安装的应用程序.当然,这可以通过简单地保留已安装的应用程序列表来实现.但这不应该是必要的! 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);
使用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);
差异
>使用意图时,本地程序包管理器不知道安装源自哪个应用程序.具体来说,getInstallerPackageName(…)返回null.
>隐藏方法installPackage(…)将安装程序包名称作为参数,并且很可能能够设置此值.
题
是否可以使用意图指定包安装程序名称?
(也许安装程序包的名称可以作为安装意图的额外添加?)
提示:如果要下载Android源代码,可以按照此处描述的步骤操作:下载源树.要提取* .java文件并根据包层次结构将它们放在文件夹中,您可以查看这个简洁的脚本:View Android Source Code in Eclipse.
解决方法:
目前第三方应用程序无法使用此功能.请注意,即使使用反射或其他技巧来访问installPackage()也无济于事,因为只有系统应用程序才能使用它. (这是因为它是低级别的安装机制,在用户批准权限之后,因此常规应用程序无法安全访问.)
此外,installPackage()函数参数经常在平台版本之间发生更改,因此您尝试访问它的任何内容都将在该平台的各种其他版本上失败.
编辑:
另外值得指出的是,这个installerPackage最近才被添加到平台(2.2?),并且最初并未用于跟踪安装应用程序的用户 – 平台使用它来确定在报告错误时启动谁该应用程序,用于实现Android反馈. (这也是API方法参数发生变化的次数之一.)至少在推出它之后很长一段时间,Market仍然没有使用它来跟踪它已经安装的应用程序(它可能仍然没有使用它),但只是用它来设置Android反馈应用程序(与市场分开)作为“所有者”来处理反馈.
内容总结
以上是互联网集市为您收集整理的android – 以编程方式安装/卸载APK(PackageManager vs Intents)全部内容,希望文章能够帮你解决android – 以编程方式安装/卸载APK(PackageManager vs Intents)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。