Share a method for installing android silently and restarting the app after installation. androidapp

Source: Internet
Author: User

Share a method for installing android silently and restarting the app after installation. androidapp

I. Requirements

Previously, the boss raised a requirement that an app running on an advertisement machine must be automatically upgraded. The advertisement machine is non-touch screen and cannot be manually clicked. Therefore, the app must be automatically downloaded, automatic Installation and upgrade. After the installation is complete, the app will continue to run. It is best not to use other apps to implement the above functions.

II. Implementation ideas

The first method to implement this function is Silent Installation. Because the advertisement machine is already root, the silent installation is smooth. The main code for installing the app is as follows:

/*
@ Pararm apkPath: full path of the app to be installed, for example:/sdcard/app/app.apk
**/
Private static boolean clientInstall (String apkPath) {PrintWriter = null; Process process = null; try {process = runtime.getruntime(cmd.exe c ("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. pr Intln ("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 ("apk installation exception");} finally {if (process! = Null) {process. destroy () ;}} return false ;}

The above method can be successfully installed, but the software can continue to run after the software installation is complete, because after the installation, the current app process has been killed. It is impossible to meet the requirements raised by the boss that the software runs normally after installation. If we still want to use android to implement this requirement, it cannot be implemented because the app process is killed, so we need to use a third party to start our app. The first thing I think of is that linux executes the am start command, but this command cannot be executed immediately, so sleep is needed to achieve this, the command format is as follows: sleep time (unit: seconds), am start-n. The complete code 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 = Runtime. getRuntime (); try {Process localProcess = runtime.exe c ("su"); OutputStream localOutputStream = localProcess. getOutputStream (); DataOutputStream localDataOutputStream = new DataOutputStream (localOutputStream); localDataOutputStream. writeBytes (cmd); localDataOutputStream. flush (); Logger. e ("device preparation to restart");} catch (IOException e) {Logger. I (TAG + "strLine:" + e. getMessage (); e. printStackTrace ();}}

Permissions involved:

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

Note: not all root devices can execute Process localProcess = runtime.exe c ("su"). This requires hardware support, which I have encountered. The silent installation can be achieved through the above two methods. After the installation is complete, the app automatically needs to perform the operation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.