我曾经遇到过这个问题,但我找到了解决方案,所以决定将它张贴在这里以防万一其他人需要它。
如何启动原生安装程序来安装 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/