r1k0
r1k0

Reputation: 1406

Android starting an activity with Intent.ACTION_INSTALL_PACKAGE and extra return result does not return

I am trying to do programmatic update to the application I am writing, since it is not a Google Play application and I want to provide a way to do updates.

I've been searching around and found out how to start the Android installer after I download the APK for the update, but I need to get a result from the installer, that tells me if the update succeeded or not, or if it was cancelled by the user.

I saw a bunch of questions on StackOverflow about this, and the answers usually involved using a broadcast receiver. The problem with that is that it can only receive intents about the package being installed, not about canceled installs of fails.

I did some more research and it seems the Intent API provides some extras such as Intent.EXTRA_RETURN_RESULT, which if set to true should return a result from the installer activity - I guess via onActivityResult. Unfortunately this doesn't work. Is there anybody that got this working/does it work like this?

Here is the code preparing the installer activity start, that I currently have:

            Intent installApp = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            installApp.setData(downloadedApk);
            installApp.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
            installApp.putExtra(Intent.EXTRA_RETURN_RESULT, true);
            installApp.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, context.getApplicationInfo().packageName);
            context.startActivityForResult(installApp, 1);

Upvotes: 4

Views: 4141

Answers (1)

Basic Coder
Basic Coder

Reputation: 11422

Do you use Fragments? The onActivityResult will be called from the Activity or Fragment you have called startActivity(...). Fragment#startActivity(...) does exist. Use it to get the Fragment's onActivityResult(...) called. If you are not using Fragments, this Workaround will work.

Workaround Pseudocode

// CURRENT_VERSION is a const with the current APK version as int

Activity#onStart() {
  super.onStart();
  checkForUpdaterResult();
  /*...*/
}

Activity#checkForUpdaterResult() {
  final int updateVersion = preferences.getInt(UPDATE_VERSION, -1);
  switch(updateVersion) {
    case -1:break;
    default: 
      // updateVersion = oldVersion is smaller than the new currentVersion
      boolean success = updateVersion < CURRENT_VERSION;
      onUpdaterPerformed(success, updateVersion , CURRENT_VERSION); 
    break;
  }
}

Activity#startUpdate(File pAPK) {
  perferences.putInt(UPDATE_VERSION, CURRENT_VERSION);
  /*...*/
}

Activity#onUpdaterPerformed(boolean pSuccess, int pFromVersion, int pToVersion) {
  Toast.show("Update success: " + pSuccess);
  /* e.g. migrate DB */
  /*...*/
}

Upvotes: 3

Related Questions