I have tried many ways given in StackOverFlow and other website but it doesn't really work.
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.
This is my 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);
}
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?
Rather than setting an alarm, have a Manifest registered BroadcastReceiver specifically for this purpose. It can either listen to android.intent.action.PACKAGE_REPLACED (note: the word action is missing from this constant) or to android.intent.action.MY_PACKAGE_REPLACED
<receiver android:name="...">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED"></action>
        <data android:scheme="package" android:path="com.your.package" /> 
    </intent-filter>
</receiver>
After reinstall your receiver will get this message and you'll be able to start an Activity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With