Sometimes we write the app to use Uid=0 way to start a process,framework layer and the app layer can not be done, only by writing scripts, using AM to achieve. Here are the specific steps:
1. Create a Java project that contains the main () method
1.1. Create a Java project
1.2. Add the main () method
1.3. Export as a jar package
1.4. Turn the Java version jar into the Android version of the jar
First, locate the folder where the DX tool is located, such as android-sdk/build-tools/20.0.0, and add the folder to the environment variable path;
Second, execute the compile command dx--dex--output=classes.dex Bktools.jar
Finally, package the Dex file into the Android version of jar,aapt add Bktools.jar classes.dex
1.5. Push the jar package to the/system/framework directory of your phone
1.6. The permission to modify the jar package is 777
2. Write a Linux shell script
2.1. Create a new text named run_bktools.sh
Enter the following code:
# Script to start ' am ' on the device, which have a very rudimentary# shell. #base =/systemexport classpath= $base/fram ework/bktools.jarexec app_process $base/bin com.larack.bktools.BKMain "[email protected]"
Classpath is the path to the jar package, Com.larack.bktools.BKMain is the class where the jar package's main function is located, and "[email protected]" means that the current parameter is passed into main.
2.2. Push the SH file run_bktools.sh to the phone/system/bin directory, and modify the permission to 777
2.3. Test start jar package with SH script
OK, the start is successful.
3. Launch the shell script in the Android app
3.1. Create an Android project
3.2. Start the shell script with root
PackageCom.larackbkapp;ImportJava.io.DataOutputStream;Importjava.io.IOException;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Private Static FinalString TAG = "AAA"; Private Static FinalString CMD = "run_bktools.sh"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); intresult =-1; Result=execrootcmdsilent (CMD); if(-1 = =result) Toast.maketext ( This, "Start fail.", Toast.length_long). Show (); ElseToast.maketext ( This, "Start success.", Toast.length_long). Show (); } Public intexecrootcmdsilent (String cmd) {intresult =-1; DataOutputStream dos=NULL; Try{Process P= Runtime.getruntime (). EXEC ("su"); DOS=NewDataOutputStream (P.getoutputstream ()); LOG.I (TAG, CMD); Dos.writebytes (cmd+ "\ n"); Dos.flush (); Dos.writebytes ("Exit\n"); Dos.flush (); P.waitfor (); Result=P.exitvalue (); LOG.I (TAG,"Success execrootcmdsilent (" + cmd + ") =" +result); } Catch(Exception e) {e.printstacktrace (); LOG.E (TAG,"Execrootcmdsilent (" + cmd + "), Exception:" +e.getmessage ()); } finally { if(Dos! =NULL) { Try{dos.close (); } Catch(IOException e) {e.printstacktrace (); } } } returnresult; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }}
3.3. Check if the shell has been successfully called
Connect your phone to your computer, compile and execute Bkapp, and use adb logcat to check for "Success execrootcmdsilent." To see if "Start Success" is displayed on the phone.
Check OK, I will no longer.
At this point the background is also executing the code we wrote in the jar package, if we write our own activitymanagerserive,powermanagerserive in the jar package or, otherwise, it will be run as root.
Android to root a Process[shell script method]