Android requests root permissions for silent installation

Source: Internet
Author: User

These days in a new function, need to use the silent installation, so on the internet to find some of the silent installation of information here to record it. In fact, the principle of implementing a silent installation is to request the root rights of Android phone, by executing the Linux command to install the APK to the phone system, in fact, the code is not a lot, it is listed here, later use can be directly turned out:

1      Public classMyThreadextendsThread {2         PrivateString Path;3 4          PublicMyThread (String path) {5             //TODO auto-generated Constructor stub6              This. Path =path;7         }8 9 @OverrideTen          Public voidrun () { One             //TODO auto-generated Method Stub A             Super. Run (); -  -Process Process =NULL; theOutputStream out =NULL; -InputStream in =NULL; -  -             Try { +Process = Runtime.getruntime (). EXEC ("su"); -out =Process.getoutputstream (); +  AOut.write (("PM Install-r" + path + "\ n"). GetBytes ()); atin =Process.getinputstream (); -                 intLen = 0; -                 byte[] bs =New byte[256]; -  -                  while( -1! = (len =In.read (BS))) { -String State =NewString (BS, 0, Len); in                     if(State.equals ("success\n")) { -  toMessage m =NewMessage (); +M.ARG1 = 1; -Downloadservice. This. Myhandler.sendmessage (m); the  *                     } $                 }Panax Notoginseng}Catch(IOException e) { -                 //TODO auto-generated Catch block the e.printstacktrace (); +  A             } the         } +}

The main steps are:

1. Execute the SU command of Android phone, if the phone is already root, the Android phone will usually have a prompt box to indicate whether you give root permission,

2. If Android has root privileges and agrees to the app's permissions, the PM command is called to install

3. After the installation is completed, the output stream of the command can be read to determine if the application is successfully installed, and a "success" character is generally obtained.

Problems that arise:

1. If the user refuses to grant root permission, there is no way to correctly judge

2. If the user refuses to grant root permission, the read error stream can be judged by the failure. However, if the user is given root privileges when the error stream is read, the installation operation cannot be performed properly

At present, these two questions do not know how to judge, I hope to have done a friend pointing

HTTP download apk file, the specific code is as follows:

1HttpGet HttpGet =Newhttpget (URL);2             Try {3HttpResponse HttpResponse =Newdefaulthttpclient ()4 . Execute (httpget);5 6                 if(Httpresponse.getstatusline (). Getstatuscode () = = 200) {7InputStream is =httpresponse.getentity (). getcontent ();8                     //start download apk file9FileOutputStream fos =NewFileOutputStream (DownloadpathTen+ "/" + name+ ". apk"); One                     byte[] buffer =New byte[8192]; A                     intCount = 0; -                      while((count = is.read (buffer))! =-1) { -Fos.write (buffer, 0, count); the                     } - fos.close (); - is.close (); -LOG.D (TAG, "apk path =" + Downloadpath + "/" + name+ ". apk")); +  -                     FinalString Path = Downloadpath + "/" + name+ ". apk"; +  ATimer =NewTimer (); atTimer.schedule (NewTimerTask () { - @Override -                          Public voidrun () { -                             //TODO auto-generated Method Stub -  -Message m =NewMessage (); inM.ARG1 = 2; -Bundle bundle =NewBundle (); toBundle.putstring ("Path", path); + M.setdata (bundle); -Downloadservice. This. Myhandler.sendmessage (m); the                         } * }, TimeOut); $ Panax Notoginseng                     NewMyThread (Path). Start (); -                 } the}Catch(Exception e) { +}

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.