有没有办法在APK安装失败时收到通知?

dor*_*506 5 installation android apk

我正在写一个应用程序。在应用程序中,我以编程方式下载 APK 并启动启动 APK 安装的意图。

Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")),
                "application/vnd.android.package-archive");
        startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

一切都运转良好。

但是,例如,当我尝试启动损坏的 APK 的安装时,会出现一个对话框: 在此输入图像描述

我希望在安装 APK 时出现问题(APK 损坏、空间不足等...)时收到通知。

那么,有没有一种方法(事件或其他东西)可以收到通知,以便我可以用我自己的对话框替换“解析错误”默认对话框?

谢谢!

小智 5

我不能 100% 确定这种方法是否适用于所有可能的情况,但我一直在测试并且至少对我有用。

我没有找到有关检测 Android 中安装错误的信息,因此我尝试先发制人地检测 apk 文件是否已损坏。

根据此链接,apk 只是带有 dalvik 代码的 jarfile。因此,您可以使用 zip 解压缩来检测文件是否已损坏以及解析包时是否存在任何问题。

boolean corruptedApkFile = false;
try {
     new JarFile(updateFileFullPath); //Detect if the file have been corrupted
} catch (Exception ex) {
     corruptedApkFile = true;
}
Run Code Online (Sandbox Code Playgroud)