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
- PM Install Package
- 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