【问题标题】:How to launch the stock installer app in android如何在 android 中启动股票安装程序应用程序
【发布时间】:2012-05-02 03:04:31
【问题描述】:

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

如何启动本机安装程序来安装 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://

开头

干杯。

【问题讨论】:

  • @CommonsWare - 您的链接确实包含此信息,但并未强调“file://”方案的重要性。顺便说一句,谢谢你的链接,我希望我能早点找到它——这样可以节省我一堆白发:)

标签: android android-intent installation protocols


【解决方案1】:

实际上,您可以简单地使用Uri 类的fromFile(...) 方法而不是使用parse(...) 方法(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);

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2012-08-06
    相关资源
    最近更新 更多