How to obtain root permissions for Android applications

Source: Internet
Author: User

Recently, when I was learning Android, I needed to obtain the root permission to write a small program. I have been searching for it online for a long time. I found this method is feasible. The device must have been cracked before! Execute the su command.

 

1. Create a method: the code is as follows:

 

View code

Package CN. ycmoon. utility;

Import java. Io. dataoutputstream;

Import Android. App. activity;
Import Android. util. log;

Public class systemmanager extends Activity
{
/**
* If the application runs the command to obtain the root permission, the device must be cracked (obtain the root permission)
* @ Param command: String apkroot = "chmod 777" + getpackagecodepath (); rootcommand (apkroot );
* @ Return application Yes/No get root permission
*/
Public static Boolean rootcommand (string command)
{
Process = NULL;
Dataoutputstream OS = NULL;
Try
{
Process = runtime.getruntime(cmd.exe C ("Su ");
OS = new dataoutputstream (process. getoutputstream ());
OS. writebytes (command + "\ n ");
OS. writebytes ("Exit \ n ");
OS. Flush ();
Process. waitfor ();
} Catch (exception E)
{
Log. D ("*** debug ***", "root REE" + E. getmessage ());
Return false;
} Finally
{
Try
{
If (OS! = NULL)
{
OS. Close ();
}
Process. Destroy ();
} Catch (exception E)
{
}
}
Log. D ("*** debug ***", "root suc ");
Return true;
}
}

 

2. In the mainactivity method of the application:

 

 

Public class mainactivity extends Activity
{
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
String apkroot = "chmod 777" + getpackagecodepath ();
Systemmanager. rootcommand (apkroot );
}
}

 

In this way, when the application is running, the message dialog box "the application has obtained the root permission" appears"

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.