2

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?

NYCHippo
  • 377
  • 1
  • 4
  • 14

2 Answers2

6

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.

NYCHippo
  • 377
  • 1
  • 4
  • 14
0

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.)

Community
  • 1
  • 1
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • Thank you, Paul, but I figured out how to do this. See the "addFlags" line, which I added above. It causes an open dialog to pop up after the installation dialog. At that point, I select "Open", and my app restarts. This is sufficient for my needs. – NYCHippo Mar 24 '13 at 16:22
  • Good to hear it! You should move the change into a separate answer and mark it answered so that others with the same problem can see what the problem was and how it was resolved. – Paul Lammertsma Mar 24 '13 at 16:41
  • Yes, I'll move the change in 5-6 hours, when the software here permits me to answer my own question. – NYCHippo Mar 24 '13 at 18:10