The final solution and analysis of the problem of obtaining root permissions by Android programming in the previous article

Source: Internet
Author: User

To facilitate the previous article address: <a href = "http://www.bkjia.com/kf/201203/124585.html>
After several days of calling the root permission, the problem was finally solved. Baidu, Google, and all the methods were tested.
I have summarized three methods for obtaining root permissions through tests over the past few days:
1 method referenced in the previous article
[Html]
</Pre> <pre>
[Html]
Public class DemoActivity extends Activity {
Public final String rootPowerCommand = "chmod 777/dev/block/mmcblk0"; // grant the root permission command
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
New AlertDialog. Builder (this). setMessage (rootCommand (rootPowerCommand) + "..."). show ();
File [] files = new File ("/root"). listFiles ();
If (files = null) {// <strong> <span style = "font-size: 18px; color: # ff0000;"> it indicates NULL .... That is, you cannot access the files under it.
</Span> </strong> new AlertDialog. Builder (this). setMessage (". OK..."). show ();
}
// Files [0]. getName ();
}
/**
* Grant root User Permissions
*
* @ Param command
**/
Public boolean rootCommand (String command ){
Process process = null;
DataOutputStream dos = null;
Try {
Process = runtime.getruntime(cmd.exe c ("su ");
Dos = new DataOutputStream (process. getOutputStream ());
Dos. writeBytes (command + "\ n ");
Dos. writeBytes ("exit \ n ");
Dos. flush ();
Process. waitFor ();
} Catch (Exception e ){
Return false;
} Finally {
Try {
If (dos! = Null ){
Dos. close ();
}
Process. destroy ();
} Catch (Exception e ){
}
}
Return true;
}
 
}
[Java]
<Span style = "font-family: Arial; BACKGROUND-COLOR: # ffffff"> </span> where I use dos. writeBytes ("exit \ n"); removed, and found that the mobile phone prompts that the permission is successfully obtained, but the problem is that the mobile phone is black, the program is still running, that is, the black screen ..... Wait for the next hop to see if it is forced to close. The reason is as follows or an explanation.
[Java]
<Strong> <span style = "font-size: 18px;"> <span style = "color: # 3333ff; "> 2 <span class =" link_title "> <a title =" RootExplorer how to get root permissions "href =" http://blog.csdn.net/a345017062/article/details/6441986 "> RootExplorer get root permissions </a> method (the following is the source code from RootExplorer) </span> </strong>
[Html]
<Span style = "font-size: 18px;"> <strong> <span style = "BACKGROUND-COLOR: #000000 "> ProcessBuilder pb = new ProcessBuilder ("/system/bin/sh ");
// Java. lang. ProcessBuilder: Creates operating system processes.
Pb. directory (new File ("/"); // you can specify the current directory of the shell.
Try {
Process proc = pb. start ();
// Obtain the input stream, which can be used to obtain the SHELL output.
BufferedReader in = new BufferedReader (new InputStreamReader (proc. getInputStream ()));
BufferedReader err = new BufferedReader (new InputStreamReader (proc. getErrorStream ()));
// Obtain the output stream, which can be used to send commands to SHELL.
PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (proc
. GetOutputStream (), true );
Out. println ("pwd ");
Out. println ("su root"); // when this sentence is executed, a dialog box is displayed (the following program requires the highest permission to be granted...), asking the user to confirm.
Out. println ("cd/data"); // This directory must have the root permission in the system for access.
Out. println ("ls-l"); // if this command can list the data file storage directory of the currently installed APK, it means that we have the ROOT permission.
Out. println ("exit ");
// Proc. waitFor ();
String line;
While (line = in. readLine ())! = Null ){
System. out. println (line); // print the output result.
}
While (line = err. readLine ())! = Null ){
System. out. println (line); // print the error output
}
In. close ();
Out. close ();
Proc. destroy ();
} Catch (Exception e ){
System. out. println ("exception:" + e );
</Span >}</strong> </span>
[Java]
After my tests, it is also feasible, but the problem is still the same, that is, black screen, and sometimes jump out of the force to close the program.
[Java]
<Span style = "font-size: 18px; "> <strong> 3 from Google </strong> </span> <a href =" http://code.google.com/p/superuser/ "> <span style =" font-size: 18px; "> <strong> http://code.google.com/p/superuser/ </strong> </span> </a>, <em> <strong> (about </strong> <span style =" font-size: 16px; color: #000000; "> Superuser super manager is Baidu. The following is his source code for obtaining root permissions </span> </em>
[Java]
 
[Html]
File superuser = new File ("/system/bin/superuser ");

If (superuser. exists ())
{
// Return device to original state
Process process = runtime.getruntime(cmd.exe c ("superuser ");
DataOutputStream OS = new DataOutputStream (process. getOutputStream ());
OS. writeBytes ("mount-oremount, rw/dev/block/mtdblock3/system \ n ");
OS. writeBytes ("busybox cp/system/bin/superuser/system/bin/su \ n ");
OS. writeBytes ("busybox chown 0: 0/system/bin/su \ n ");
OS. writeBytes ("chmod 4755/system/bin/su \ n ");
OS. writeBytes ("rm/system/bin/superuser \ n ");
OS. writeBytes ("exit \ n ");
OS. flush ();
}
[Java]
I tested this method, but there is no way to change it. It is not recommended to use it. Of course you can study it.
[Java]

[Java]
Now, let's explain why the program is black. From the above description, we can know that the program must be running !!!! Why is it not displayed? The key point is here. Let's think about the threads we used to write when learning javaSE. If we didn't learn about writing interfaces and threads, we often encounter a Button) is the interface stuck? Let me talk about it myself. When I create a QQ-like application, the server gets a button and click it to start the server. But when I click it, it gets stuck. Even though the server is up, however, the server cannot be shut down. The server startup and shutdown interfaces are dead (just like the Android black screen and display forced shutdown ), at that time, we obviously thought of the thread method to start the server !! So this idea should be maintained now --! Next, let's not explain it. Everyone understands it! Get a thread .. Solve it .....
[Java]
This problem has been debugged for a long time, and n methods have been implemented. The simulator does not have root (I do not know how many times it has been debugged by plugging the phone... --#). What are the shortcomings? Please add more !!

From Wei Yirong's column

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.