How to implement app self-update from downloaded apk?

876 Views Asked by At

I'm working with OTA update for the app. It works like this: I click "check update", it checks it, downloads if exists and save apk on the device. Then I can install it, but of course I have a confirmation dialog. I need to do it silently and restart the app.

I want to make it auto-install when downloading finished. So I just click and if there's some update the app restarts in new version. I can't figure out how to do it. The device is rooted.

2

There are 2 best solutions below

0
On BEST ANSWER

The following code works only on roodted devices!

private int installApk(File file) {
    if (!file.exists()) throw new IllegalArgumentException();

    Process process = null;

    try {
        process = Runtime.getRuntime().exec(new String[]{"su", "-c", "pm install -r " + file
                .getAbsolutePath()});
        int exitCode = process.waitFor();
        if (exitCode != 0) throw new RuntimeException();
    } catch (IOException | InterruptedException exception) {
        Log.w(getClass().getSimpleName(), exception);
    }

    return (process == null) ? Integer.MIN_VALUE : process.exitValue();
}
0
On

You cannot have this for security reasons. No user app can install silently on non rooted phone.

Only thing you can do is tell user you just downloaded the update and ask if a/he wants to install it. If so, then fire installation intent and have your user proceed with installation.