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"