Android gets the root permission in apk and runs the command

Source: Internet
Author: User

In the apk, sometimes the root permission is required, such as updating system files such as system files through the apk, to avoid upgrading the firmware or directly accessing certain devices in the apk. The following method is used to obtain the root permission in apk, provided that the device has already been root.
The key point is that the following statement generates a process with root permission by executing su:
Process p = runtime.getruntime(cmd.exe c ("su ");
Then, write the command to be executed to the process to run the command as root:
Dos = new DataOutputStream (p. getOutputStream ());
Dos. writeBytes (cmd + "\ n ");
Dos. flush ();
Or use the following method:
Runtime.getruntime(cmd.exe c (new String [] {"/system/bin/su", "-c", cmd });

After testing, run the command with root permission. The test is successful only on the real machine, but not on the simulator.

During the first running, the page for requesting root permissions appears. Select and remember, and allow:


The test program interface. If it is already root, the corresponding device node of the/system partition is displayed:

Main file: RootCmd. java
[Java]
Package org. ckl. root;
 
Import java. io. DataInputStream;
Import java. io. DataOutputStream;
Import java. io. IOException;
 
Import android. util. Log;
 
Public final class RootCmd {
 
Private static final String TAG = "RootCmd ";
Private static boolean mHaveRoot = false;
 
// Determine whether the Android server has the root permission, that is, whether the root permission is obtained.
Public static boolean haveRoot (){
If (! MHaveRoot ){
Int ret = execRootCmdSilent ("echo test"); // you can run the test command to detect
If (ret! =-1 ){
Log. I (TAG, "have root! ");
MHaveRoot = true;
} Else {
Log. I (TAG, "not root! ");
}
} Else {
Log. I (TAG, "mHaveRoot = true, have root! ");
}
Return mHaveRoot;
}
 
// Execute the command and output the result
Public static String execRootCmd (String cmd ){
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;

Try {
Process p = runtime.getruntime(cmd.exe c ("su"); // The Root-processed android system has the su command.
Dos = new DataOutputStream (p. getOutputStream ());
Dis = new DataInputStream (p. getInputStream ());
 
Log. I (TAG, cmd );
Dos. writeBytes (cmd + "\ n ");
Dos. flush ();
Dos. writeBytes ("exit \ n ");
Dos. flush ();
String line = null;
While (line = dis. readLine ())! = Null ){
Log. d ("result", line );
Result + = line;
}
P. waitFor ();
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
If (dos! = Null ){
Try {
Dos. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
If (dis! = Null ){
Try {
Dis. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
Return result;
}
 
// Execute the command but ignore the result output
Public static int execRootCmdSilent (String cmd ){
Int result =-1;
DataOutputStream dos = null;

Try {
Process p = runtime.getruntime(cmd.exe c ("su ");
Dos = new DataOutputStream (p. getOutputStream ());

Log. I (TAG, cmd );
Dos. writeBytes (cmd + "\ n ");
Dos. flush ();
Dos. writeBytes ("exit \ n ");
Dos. flush ();
P. waitFor ();
Result = p. exitValue ();
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
If (dos! = Null ){
Try {
Dos. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
Return result;
}
}

Related Files: SystemPartition. java, get the/system partition device node, and support re-mount/system to read/write:
[Java]
Package org. ckl. root;
 
Import java. io. DataInputStream;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. IOException;
 
Import android. util. Log;
 
Public class SystemPartition {
Private static final String TAG = "SystemMount ";
Private static String TMP_PATH = "/sdcard/mount.txt ";
Private static String mMountPiont = null;
Private static boolean mWriteable = false;

Private SystemPartition (){
Log. I (TAG, "new SystemMount ()");
}

Private static class SystemPartitionHolder {
Private static SystemPartition instance = new SystemPartition ();
}

Public SystemPartition getInstance (){
Return SystemPartitionHolder. instance;
}

Public static String getSystemMountPiont (){
DataInputStream dis = null;
If (mMountPiont = null ){
Try {
RootCmd.exe cRootCmd ("mount>" + TMP_PATH );
// Runtime.getruntime(cmd.exe c ("mount>" + TMP_PATH );

Dis = new DataInputStream (new FileInputStream (TMP_PATH ));

String line = null;
Int index =-1;
While (line = dis. readLine ())! = Null ){
Index = line. indexOf ("/system ");
If (index> 0 ){
MMountPiont = line. substring (0, index );
If (line. indexOf ("rw")> 0 ){
MWriteable = true;
Log. I (TAG, "/system is writeable! ");
} Else {
MWriteable = false;
Log. I (TAG, "/system is readonly! ");
}
Break;
}
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
If (dis! = Null ){
Try {
Dis. close ();
} Catch (IOException e1 ){
E1.printStackTrace ();
}
Dis = null;
}

File f = new File (TMP_PATH );
If (f. exists ()){
F. delete ();
}
}
}

If (mMountPiont! = Null ){
Log. I (TAG, "/system mount piont:" + mMountPiont );
} Else {
Log. I (TAG, "get/system mount piont failed !!! ");
}

Return mMountPiont;
}

Public static boolean isWriteable (){
MMountPiont = null;
GetSystemMountPiont ();
Return mWriteable;
}

Public static void remountSystem (boolean writeable ){
String cmd = null;
GetSystemMountPiont ();
If (mMountPiont! = Null & RootCmd. haveRoot ()){
If (writeable ){
Cmd = "mount-o remount, rw" + mMountPiont + "/system ";
} Else {
Cmd = "mount-o remount, ro" + mMountPiont + "/system ";
}
RootCmd.exe cRootCmdSilent (cmd );

IsWriteable ();
}
}
}

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.