To grant the root permission to the Android app, the Android device must have the root permission.
How an application obtains root permissions: grant the highest permissions to the Code Execution directory of the application. In Linux, use chmod 777 [Code Execution Directory]
The Code is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 |
/**
* If the application runs the command to obtain the root permission, the device must be cracked (obtain the root permission)
*
* @ Return application Yes/No get root permission
*/
Public static Boolean upgraderootpermission (string pkgcodepath ){
Process = NULL;
Dataoutputstream OS = NULL;
Try {
String cmd = "chmod 777" + pkgcodepath;
Process = runtime.getruntime(cmd.exe C ("Su"); // switch to the root account
OS = new dataoutputstream (process. getoutputstream ());
OS. writebytes (CMD + "\ n ");
OS. writebytes ("Exit \ n ");
OS. Flush ();
Process. waitfor ();
} Catch (exception e ){
Return false;
} Finally {
Try {
If (OS! = NULL ){
OS. Close ();
}
Process. Destroy ();
} Catch (exception e ){
}
}
Return true;
} |
Call code:
1
2
3
4
5
6
7 |
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Code execution directory of the current application
Upgraderootpermission (getpackagecodepath ());
} |
After executing the above Code, the system will pop up the dialog box "whether to allow the root permission to be obtained". Then, select allow.