从代码启动 .apk

Launching .apk from Code

Intent install_intent = new Intent(Intent.ACTION_VIEW);
install_intent.setDataAndType(Uri.fromFile(new File(Environment
      .getExternalStorageDirectory() 
          + "app-release.apk")),
          "application/vnd.android.package-archive");
startActivity(install_intent);

因此,我正在创建一个在启动时检查更新的应用程序。它从某个位置下载文件,然后将此意图启动到 运行 .apk。不幸的是,当我这样做时,它给了我错误 "there is a problem parsing the package android studio".

如果我导航到下载路径,然后 运行 手动安装 .apk,则可以正常安装。

有什么想法吗?

试试这个:

File apkFile = new File(Environment.getExternalStorageDirectory()
                  .getAbsolutePath() + "/app-release.apk");
Intent install_intent = new Intent(Intent.ACTION_VIEW);
install_intent.setDataAndType(Uri.fromFile(apkFile), 
     "application/vnd.android.package-archive");
startActivity(install_intent);