我有一个名为InstallationHelper的Activity,其工作是从文件系统安装下载的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等任何其他过滤器。)代码与默认包安装程序一起使用和Lookout,但是当选择“验证并安装”时,会立即调用onActivityResult(),然后才能进行安装。这是日志:
使用“包安装程序”:
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的活动正在最终确定并传递给PackageVerificationService,因此我的onActivityResult()会过早被调用?有办法防止这种情况吗?