It's a good experience that Android phones can invoke commands silently to install software after it gets root, but the current online code for silent installation of Android apps comes from one person, with a very SB bug, and no one who borrows the code has found it. The code that caused almost all the silent installation of Android apps on the network was wrong.
The code is as follows |
Copy Code |
New Thread () { public void Run () { Process process = NULL; OutputStream out = null; InputStream in = null; try { Request Root Process = Runtime.getruntime (). EXEC ("su"); out = Process.getoutputstream (); Invoke installation Out.write (("PM Install-r" + Currenttempfilepath +) "). GetBytes ()); in = Process.getinputstream (); int len = 0; Byte[] bs = new byte[256]; while ( -1!= (len = In.read (BS))) { String state = new String (BS, 0, Len); if (State.equals ("Success ")) { Operation after the installation is successful } } 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 (); |
This code can actually run, in the root of the phone can also be silently installed successfully, but the code has a problem is a bug, do not know which day the problem of the code will be wrong
The following is a modified code that modifies the bug above
The code is as follows |
Copy Code |
New Thread () { public void Run () { Process process = NULL; OutputStream out = null; InputStream in = null; try { Request Root Process = Runtime.getruntime (). EXEC ("su"); out = Process.getoutputstream (); Invoke installation System.out.println (Apkfile.getabsolutepath ()); Out.write ("PM Install-r" + apkfile.getabsolutepath () + " "). GetBytes ()); in = Process.getinputstream (); int len = 0; int readlen = 0; Byte[] bs = new byte[256]; read out all the output data while ( -1!= (Readlen = In.read (BS))) { Len = len + Readlen; If the read data is greater than the buffer. Is stopped if (Len > Bs.length) { Len-= Readlen; Break } } String state = new String (BS, 0, Len); if (State.startswith ("Success")) { Operation after the installation is successful } else { Silent installation failed with manual installation Installbyuser (); } 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 (); |
The change is to read the output data from the output stream, it can be seen that if the output of the data is greater than the buffer size (256), will cause the operation after installation many times, the above error code can be run, is the output stream output is success, less than 256, but if the operation of the command error , return a big push error, the above section will read all the data, read each one to compare.