1: Requirements introduction
The boss previously proposed a requirement that the app running on the advertising machine needs to complete the automatic upgrade function. The advertising machine is non-touch screen and cannot be manually clicked, so the app must be automatically downloaded, automatically installed and upgraded, and after the installation is completed, the app must continue to run, it is best not to use other apps to realize the above functions.
Two: Implementation ideas
The first way to implement this function is to install it silently. Since the advertising machine has been rooted, the silent installation is relatively smooth. The main code for installing the app is as follows:
/* @pararm apkPath The full path of the app waiting to be installed, such as: /sdcard/app/ **/ private static boolean clientInstall(String apkPath) { PrintWriter PrintWriter = null; Process process = null; try { process = ().exec("su"); PrintWriter = new PrintWriter(()); ("chmod 777 " + apkPath); PrintWriter .println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib"); ("pm install -r " + apkPath); // ("exit"); (); (); int value = (); ("Silent installation return value:"+value); return returnResult(value); } catch (Exception e) { (); ("Exception occurred when installing apk"); } finally { if (process != null) { (); } } return false; }
The above method can be installed smoothly, but it cannot achieve that the software can continue to run after the software is installed, because after installation, the current app process has been killed. The boss's requirement is not implemented, and the software runs normally after installation. At this time, if we still think about using Android to implement this requirement, it is impossible to achieve, because the app process is killed, so we need to use a third party to start our app. The first thing I thought of is linux to execute the am start command, but this command cannot be executed immediately, so sleep is needed to implement this requirement. The command format is as follows:
private void execLinuxCommand(){ String cmd= "sleep 120; am start -n package name/package name.name of the first activity"; //Runtime object Runtime runtime = (); try { Process localProcess = ("su"); OutputStream localOutputStream = (); DataOutputStream localDataOutputStream = new DataOutputStream(localOutputStream); (cmd); (); ("The device is ready to restart"); } catch (IOException e) { (TAG+"strLine:"+()); (); } }
Permissions involved:
<uses-permission android:name=".INSTALL_PACKAGES" />
Note: Not all rooted devices can executeProcess localProcess = ("su")
;This requires hardware support, I have encountered this pitfall. Through the above two methods, silent installation can be achieved. After the installation is completed, the app automatically needs to be carried out.
Summarize
The above is the method of silently installing and rebooting the APP after Android programs are introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!