Android Silent install software app code (test available)

Source: Internet
Author: User
Tags error code flush

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.

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.