Android programming to obtain the root permission of the mobile phone

Source: Internet
Author: User

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;
}
 
}

Recently, I wrote another file manager that requires the root permission to access the folder. Although the permission is obtained successfully, it still cannot access the folder that requires the root permission, my classmates wrote the same method to obtain permissions, which is strange .. I put it on a mobile phone that has already been root for testing. I didn't prompt to get the root, So I reported an error directly ....
 
Today I changed it to 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 ..... And so on... What does this sentence mean ???? Thank you for your suggestions.

Please explain ......


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.