There is the following code:

Process p = Runtime.getRuntime().exec("su" + "\n"); OutputStream i8 = p.getOutputStream(); i8.write(("pm install " + myApkFile.getAbsolutePath() + "\n" + "reboot" + "\n" + "exit" + "\n").getBytes("UTF-8")); i8.flush(); p.waitFor(); 

He installs the application. But there is a problem. The application does not have time to install, as soon as the reboot command is executed, restarting the phone. It would be nice if you first install the "pm install ..." application, wait for p.waitFor(); and then "reboot" , but it was not there. The application is updated in this way, that is, it replaces itself with a new application ( myApkFile ). If I first do "pm install ..." , then p.waitFor(); , and only then "reboot" , then nothing will come of it. If the application is replaced, then all threads, Activity , Service , Receiver and so on will be interrupted. Thus, the "reboot" command will also be unreachable (unreachable code).

Now the question. Is it possible to do something like the following su script:

 pm install myApkFile.apk sleep(30 секунд) reboot exit 

Is it possible to somehow put this 30-second delay? I think during this time the phone will have time to install the application, and then reboot.


The "reboot" command of rebooting the phone is necessary, as there are some Services that are only launched from the BroadcastReceiver , which is activated using <action android:name="android.intent.action.BOOT_COMPLETED" /> .

  • What prevents you from running the service directly after installation? - Pavel Mayorov
  • @PavelMayorov, how will the application launch itself after installation? - nick
  • And how do you start the installation? - Pavel Mayorov
  • @PavelMayorov, in the sense of how? Through Runtime...exec("su")...pm install myAPK.apk... The question is all written. - nick

2 answers 2

30 second pause. I advise you to run this case in ProgressDialog .

 TimeUnit.SECONDS.sleep(30); 
  • I didn’t quite understand why ProgressDialog - nick
  • Well, so that the user knows that the device will restart. And then he will start to climb and bang, he passed out - Flippy
  • After the installation is completed, the application closes, as the system replaces it with a new one. All threads and ProgressDialogи will be forcibly destroyed. - nick
  • Aaa) Ie you need to update the application and the reboot occurred 30 seconds after that? - Flippy
  • to the point (7 characters needed) - nick
 pm install myApkFile.apk sleep 30 reboot exit 
  • @NicolasChabanovsky, yes. (You can receive your answer in 11 hours) - nick
  • Got it. Thank! - Nicolas Chabanovsky