Android Silent Installation

Source: Internet
Author: User

Sometimes we need a silent installation of the software, but Android does not provide the appropriate API, but we know that the command line when installing Android will not prompt the user, all to implement this function, we can be implemented from the execution of the command line. The installation commands provided by Android are

    1. PM Install Package
    2. PS:PM install/sdcard/android.apk
?

However, executing this command requires a prerequisite, either a system-level application or a root permission. We'll start with the root permission to do this.

1, install the apk silently by getting root privileges
Look at the code:

    NewThread () { Public voidRun () {process process=NULL; OutputStream out=NULL; InputStream in=NULL; Try {         //Request RootProcess = Runtime.getruntime (). EXEC ("su"); out=Process.getoutputstream (); //Call InstallationOut.write ("PM Install-r" + Currenttempfilepath + "\ n"). GetBytes ()); Inch=Process.getinputstream (); intLen = 0; byte[] bs =New byte[256];  while( -1! = (len =In.read (BS))) {String State=NewString (BS, 0, Len); if(State.equals ("success\n")) {            //operation after successful installation             }            }         } Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } finally {             Try {                 if(Out! =NULL) {Out.flush ();                 Out.close (); }                 if(In! =NULL) {in.close (); }             } Catch(IOException e) {e.printstacktrace (); }}}}.start (); 

Of course it can be implemented by the NDK, and the code will not be released.

The second way, the same is done by the PM command, do not request root, but requires the system's signature. Here is the signature of the simulator, installed on the emulator with this signature apk to achieve the effect of silent installation without requesting root.

?
    //first add attributes to the manifest tagAndroid:shareduserid= "Android.uid.system"NewThread () { Public voidRun () {process process=NULL; InputStream in=NULL; Try {         //Request RootProcess =Runtime.getruntime (). EXEC ("PM Install-r" + Currenttempfilepath + "\ n"); Inch=Process.getinputstream (); intLen = 0; byte[] bs =New byte[256];  while( -1! = (len =In.read (BS))) {String State=NewString (BS, 0, Len); if(State.equals ("success\n")) {            //operation after successful installation             }            }         } Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } finally {             Try {                 if(In! =NULL) {in.close (); }             } Catch(IOException e) {e.printstacktrace (); }}}}.start (); 

Android Silent Installation

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.