Share an android silent installation and restart the app after installation

Keywords: Android Linux

I. demand introduction

Previously, boss put forward a requirement that the app running on the advertiser needs to complete the automatic upgrade function. The advertiser is non touch screen and cannot be clicked manually. Therefore, the app must be downloaded automatically and installed and upgraded automatically. After the installation, the app will continue to run. It is better not to use other apps to achieve the above functions.

II. Implementation ideas

The first way to realize this function is to install silently. Because the advertiser is already root, the silent installation is relatively smooth. The main code for installing the app is as follows:

apkPath The full path of the app to be installed, such as: / sdcard/app/app.apk
static boolean clientInstall(String apkPath) { PrintWriter PrintWriter = null; Process process = null; try { process = Runtime.getRuntime().exec("su"); PrintWriter = new PrintWriter(process.getOutputStream()); PrintWriter.println("chmod 777 " + apkPath); PrintWriter .println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib"); PrintWriter.println("pm install -r " + apkPath); // PrintWriter.println("exit"); PrintWriter.flush(); PrintWriter.close(); int value = process.waitFor(); Logger.e("Silent installation return value:"+value); return returnResult(value); } catch (Exception e) { e.printStackTrace(); Logger.e("install apk Exception occurred"); } finally { if (process != null) { process.destroy(); } } return false; }

The above methods can be successfully installed, but the software can not continue to run after the completion of the software installation, because after the installation, the current app process has been killed. It is impossible to realize the requirement of normal operation of the software after the installation put forward by boss. At this time, if we still want to use android to realize this requirement, it cannot be realized. Because the app process is killed, we need to start our app with the help of a third party, The first thing I think about is that linux executes the am start command, but this command can't be executed immediately, so it needs sleep to realize this demand. The command format is as follows: sleep time (unit: Second), am start-n, and the complete code is as follows:

private void execLinuxCommand(){ 
       String cmd= "sleep 120; am start -n Package name/Package name.first Activity Name of";
        //Runtime object
        Runtime runtime = Runtime.getRuntime();
        try {
            Process localProcess = runtime.exec("su");
            OutputStream localOutputStream = localProcess.getOutputStream();
            DataOutputStream localDataOutputStream = new DataOutputStream(localOutputStream);
            Logger.e("Device ready to restart");
        } catch (IOException e) {


Permissions involved:

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

Note: not all root devices can execute Process localProcess = runtime.exec("su"); this requires hardware support. I have encountered this pit. Through the above two methods, the silent installation can be realized. After the installation, the app automatically needs to meet the requirements.

Posted by deadimp on Sat, 02 May 2020 13:32:46 -0700