4

How to detect the situation when a user cancels an application (.apk file) installation?

My app programmatically installs other apps, but users can cancel installation process. During the installation a user sees a dialog like that: "Replace application. The application you are installing will replace another application". And then a dialog "Do you want to install this application?". If the user presses "Install" the ACTION_PACKAGE_... broadcast is generated. But how to detect if the user presses "Cancel"?

Volo
  • 28,673
  • 12
  • 97
  • 125
alexeykoval
  • 91
  • 1
  • 6
  • what? please explain better... – evilone Jul 07 '11 at 20:07
  • My application installs programmatically (or update) the other applications. But during the installation a user sees a dialog like that: "Replace application. The application you are installing will replace another application". And then a dialog "Do you want to install this application?" If a user presses "Install" the ACTION_PACKAGE... is generated. But is a user presses "Cancel"? How to detect that? – alexeykoval Jul 07 '11 at 20:19
  • Did you resolved this problem? I'm having the same here. – YasuDevil Jul 18 '11 at 19:24
  • Did you solve this issue? I am also facing same. But not getting the solution. – Rakesh Apr 20 '18 at 06:16

2 Answers2

5
public class ApplicationInstaller extends Activity {

    private final static int createState = 1, installState = 2;
    private int activityState = 0, counter = 0;
    private ApplicationInstallerReceiver aIR;
    private String appName, appPath;
    private boolean result;

    //---------------------------------------------------------------------------------------------
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_installer);
        if (activityState == 0) {
            this.activityState = ApplicationInstaller.createState;
            this.appName = this.getIntent().getStringExtra("AppName");
            this.appPath = this.getIntent().getStringExtra("AppPath");
            if (this.appName == null || this.appPath == null) {
                finish();
            }
        }
    }

    //---------------------------------------------------------------------------------------------
    @Override
    public void onStart() {
        super.onStart();
        if (this.activityState == ApplicationInstaller.createState) {
            activityState = ApplicationInstaller.installState;
            aIR = new ApplicationInstallerReceiver();
            IntentFilter ifilter = new IntentFilter();
            ifilter.addAction(Intent.ACTION_PACKAGE_ADDED);
            ifilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
            ifilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
            ifilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
            ifilter.addDataScheme("package");
            registerReceiver(aIR, ifilter);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(appPath)), "application/vnd.android.package-archive");
            try {
                startActivity(intent);
            } catch (Exception e) {
                result = false;
                finish();
            }
        } else {
            finish();
        }
    }

    //---------------------------------------------------------------------------------------------
    @Override
    protected void onResume() {
        super.onResume();
        counter++;
        if (counter == 2) {
            finish();
        }
    }

    //---------------------------------------------------------------------------------------------
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (aIR != null) {
            this.unregisterReceiver(aIR);
        }
        if (activityState == installState) {
            Intent intent = new Intent(DeviceSoftWareManager.installerAction);
            intent.putExtra("Result", this.result);
            sendBroadcast(intent);
        }
    }

    //---------------------------------------------------------------------------------------------
    class ApplicationInstallerReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            result = true;
            ApplicationInstaller.this.finish();
        }
    }
}

If a user chooses "Install" Intent.ACTION_PACKAGE... actions will be generated.

If a user chooses "Don't install" an activity quits without Intent.ACTION_PACKAGE... actions.

That means "User has canceled an installation".

Volo
  • 28,673
  • 12
  • 97
  • 125
alexeykoval
  • 91
  • 1
  • 6
1

I think that you can't detect if the user uninstall or not install an app but you can check that the other apps required are installed when you start your app or after to do an action

Eclipses
  • 1,008
  • 1
  • 9
  • 17