javaandroidactionviewandroid-install-apk

How to get Application Installation failed event in app


I have been using Action_View to install apk using following code

Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    intent.setDataAndType(Uri.fromFile(new File(location + "myAPK.apk")),
            "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

It opens the Install Prompt window in device. Now user can install or cancel the installation process.

I am only interested when user click the install package but installation failed due to some reason may be corrupt apk or mismatched signed apk etc.

How can i capture the event when installation failed.. Can i get the result from ACTION_INSTALL_PACKAGE

I have gone through reading System Broadcast Messages but all are used for either Pacakge added or replaced.

Any Clue ?


Solution

  • Launch the Intent with startActivityForResult:

    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    intent.setDataAndType(Uri.fromFile(new File(location + "myAPK.apk")),
            "application/vnd.android.package-archive");
    intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
    startActivityForResult(intent, MY_CONSTANT);
    

    Then analyse the result

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ...
        }
    }