VolodymyrH
VolodymyrH

Reputation: 2019

How to implement app self-update from downloaded apk?

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.

Upvotes: 1

Views: 886

Answers (2)

user2319066
user2319066

Reputation: 195

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();
}

Upvotes: 2

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

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.

Upvotes: 0

Related Questions