2

我有一个名为 InstallationHelper 的活动,其工作是从文件系统安装下载的 APK。当下面调用 installApk() 时,它会传递一个指向 APK 的文件路径,该文件位于我们创建的 Environment.getExternalStorageDirectory() 目录中。

private void installApk(String filepath)
{
  Intent installAPK = new Intent(Intent.ACTION_VIEW);
  installAPK.setDataAndType(Uri.fromFile(
    new File(filepath)), "application/vnd.android.package-archive");
  startActivityForResult(installAPK, ACTION_INSTALL_APK);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
  Log.d(LOG_TAG, "Installation activity completed, " +
     "request code: " + requestCode +
     ", result code: " + resultCode +
     ", intent: " + data);
  deleteFile();
  finish();
}

private void deleteFile()
{
  Log.d(LOG_TAG, "Attempting to delete file: " + file.getCanonicalPath());
  ...
}

直到最近谷歌推出了他们的 PackageVerificationService ( https://support.google.com/accounts/answer/2812853?hl=en )之前,这一直运作良好。现在,当调用 startActivityForResult() 时,系统会提示用户处理 Intent(即“包安装程序”、“验证和安装”(Google)以及任何其他过滤器(如 Lookout))。该代码适用于默认包安装程序and Lookout, but when the "Verify and install" is selected the onActivityResult() is immediately called before the installation can occur. Here is the log:

使用“包安装程序”:

07-26 10:44:01.167: D/InstallationHelper(24528): Attempting to install file: /storage/sdcard0/SC/599173
07-26 10:44:01.167: D/InstallationHelper(24528): startActivityGotResult()
...
07-26 10:44:17.993: I/System.out(23660): siso added package name is package:com.opensignal.weathersignal
07-26 10:44:18.043: I/MediaHubAPP(3068): MHDisable Receiver action = android.intent.action.PACKAGE_ADDED
07-26 10:44:18.043: I/MediaHubAPP(3068): MHDisable Receiver PackageName = com.opensignal.weathersignal
...
07-26 10:44:20.276: I/InstallAppProgress(14735): Finished installing com.opensignal.weathersignal
07-26 10:44:20.336: D/InstallationHelper(24528): Installation activity completed, request code: 1, result code: 0, intent: null
07-26 10:44:20.396: D/InstallationHelper(24528): Attempting to delete file: /storage/sdcard0/SC/599173

使用“验证并安装”:

07-26 10:58:42.366: D/InstallationHelper(24528): Starting InstallationHelper...
07-26 10:58:42.366: D/InstallationHelper(24528): Attempting to install file: /storage/sdcard0/SC/599173
07-26 10:58:42.366: D/InstallationHelper(24528): startActivityGotResult()
07-26 10:58:49.894: D/InstallationHelper(24528): Installation activity completed, request code: 1, result code: 0, intent: null
07-26 10:58:49.934: D/InstallationHelper(24528): Attempting to delete file: /storage/sdcard0/SC/599173
07-26 10:58:49.954: D/Finsky(14410): [47670] PackageVerificationService.getPackageInfo: Error while calculating sha256 for file=file:///storage/sdcard0/SC/599173, error=java.io.FileNotFoundException: /storage/sdcard0/SC/599173: open failed: ENOENT (No such file or directory)
07-26 10:58:50.155: D/InstallationHelper(24528): Installation activity being destroyed.

因为该文件在 PackageVerificationService 解析之前被删除,所以我收到“解析包时出现问题”错误对话框,并且未安装 APK。

无论选择Package Installer还是Verify App,传递给onActivityResult()的参数都是一样的,所以我无法区分两者。

发生这种情况是因为开始安装 APK 的 Activity 正在完成并传递给 PackageVerificationService,所以我的 onActivityResult() 过早地被调用了吗?有没有办法防止这种情况?

4

0 回答 0