How do I programmatically update apk silently and restart the app?
<p>I have tried many ways given in StackOverFlow and other website but it doesn't really work.</p>

<p>My issue is that I have this application that I need to update and after updating, it should automatically turn the same application (updated) back on.</p>

<p>This is my code:</p>

<pre><code>private synchronized void runRootUpdate() {    
    // Install Updated APK
    String command = "pm install -r " + downloadPath + apkFile;
    Process proc = Runtime.getRuntime().exec(new String[] {"su", "-c", command});
    int test = proc.waitFor(); // Error is here.

    if (proc.exitValue() == 0) {
        // Successfully installed updated app
        doRestart()
    } else {
        // Fail
        throw new Exception("Root installation failed");
    }
}

private void doRestart() {
    Intent mStartActivity = new Intent(context, MainActivity.class);
    int mPendingIntentId = 123456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    System.exit(0);
}
</code></pre>

<p>Take a look at my "Error is here." My old app will installed a new updated app and kill itself, thus there is no proc.waitFor() and end up back in the home screen BUT the new updated app is being installed. However, I need it to turn it back on itself. Is there any way I could do that?</p>