NYCHippo
NYCHippo

Reputation: 377

Reinstall running android app's own APK and then restart app?

From within a running Android app, I'd like to request that this same app (1) re-download itself from my private web server, (2) reinstall itself, and then (3) restart itself after the reinstallation.

I know how to perform steps 1 and 2, but I haven't figured out how to perform step 3.

After the download, I do step 2 like this (where this.apkpath has previously been set to the full pathname of the downloaded APK on my sdcard):

try {
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(Uri.fromFile(new File(this.apkpath)),
                        "application/vnd.android.package-archive");
  this.activity.startActivity(intent);
}
catch (Throwable t) {
  // handle exceptions
}

After this code succeeds, an installation confirmation dialog pops up on my screen, and the reinstallation takes place upon this confirmation. However, after installation, control returns to my desktop manager, and I have to manually restart my newly reinstalled app.

What can I do programmatically to force the app to be automatically restarted after this reinistallation?

Upvotes: 2

Views: 2284

Answers (2)

NYCHippo
NYCHippo

Reputation: 377

I figured out how to restart the app after reinstallation. In case it helps others, here's how I did it (note the added "addFlags" method call before startActivity):

try {
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(Uri.fromFile(new File(this.apkpath)),
                       "application/vnd.android.package-archive");
  // Add this line ...
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  this.activity.startActivity(intent);
}
catch (Throwable t) {
  // handle exceptions
}

When I run this, I get an open request dialog after the installation dialog. I can then click "Open", and the app will indeed restart. This is sufficient for my needs.

Upvotes: 6

Paul Lammertsma
Paul Lammertsma

Reputation: 38252

Fortunately, this isn't possible on Android 3.1 and later; you cannot simply start an application on installation. Specifically, Mark Murphy elaborates:

The application must first be invoked by the user through some sort of activity. Or, you are going to need to hook into some relevant broadcast Intent via the manifest, so you can get control when one of those events occur and kick off your service that way. Or, you are going to need to ask the user to reboot so your BOOT_COMPLETED Intent filter can get control.

In other words, you could hook into BOOT_COMPLETED and have your application launch then, but this obviously requires a reboot.

I would suggest going back to the drawing board and reconsidering what you're trying to accomplish here. If you are really set on taking this specific approach, there is this crummy workaround, but I urge you not to go that route.

An alternative approach may be scheduling an event through the AlarmManager. I'm unsure if events are removed when an application is reinstalled.

(I write "fortunately", because this behavior is inviting malicious use from malware, and also doesn't align with a user's intents.)

Upvotes: 0

Related Questions