android - 如何在 android 中启动股票安装程序应用程序

标签 android android-intent installation protocols

我曾经遇到过这个问题,但我找到了解决方案,所以决定将它张贴在这里以防万一其他人需要它。

如何启动原生安装程序来安装 apk?

很多帖子的解决方法如下:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(path), "application/vnd.android.package-archive");
context.startActivity(intent);

这很好,除了一个微小但重要的细节:

“路径”字符串必须以 file:// 开头,否则会出现类似

的异常
Unable to find an activity to handle the intent .....

因此请确保路径以 file:// 开头

干杯。

最佳答案

实际上,而不是使用 parse(...)方法,您可以简单地使用 fromFile(...) Uri 的方法类(Uri 将自动采用 "file://" 形式)。

因此:

final File file = new File(path);
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);

关于android - 如何在 android 中启动股票安装程序应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10407172/

相关文章:

android - Android 黑屏中的 Vuforia 对象识别 Unity 示例结果

android如何为progressdialog设置时间

android - 当一个 parcelable 对象通过一个 intent 传递时,它会更新对原始对象的引用吗?

c++ - 如何部署具有所有依赖项的 C++ 项目?

java - 在 Android 上使用 RESTEasyClient 时出现 NoSuchMethodError

android - 如何赋予 Android 3.2 与 Android 2.2 应用程序相同的外观?

android - 如何在启动智能手机时启动 android 应用程序并使其保持运行直到用户将其关闭?

android - 是否有用于在 Android 中查看文档(Word、Excel、Powerpoint、PDF 等)的 Intent?

python - "pip install unroll": "python setup.py egg_info" failed with error code 1

android - 关于如何安装Android的问题