android应用执行需要root权限的shell命令
导入jar包:http://blog.csdn.net/zhw1551706847/article/details/77709142
RootTools:http://blog.csdn.net/steelychen/article/details/40744305
RootTools:https://github.com/Stericson/RootTools
需要添加一个RootTools的jar包:
public class MainActivity extends AppCompatActivity { private String TAG = "MainActivity"; String commandStr = "ifconfig can0 up"; boolean ifroot; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ifroot = RootTools.isRootAvailable(); if(ifroot) Log.d(TAG, "OK"); else Log.d(TAG, "fail"); try { RootTools.getShell(true).add(command); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } catch (RootDeniedException e) { e.printStackTrace(); } } Command command = new Command(0,commandStr){ @Override public void commandCompleted(int id, int exitCode) { //命令执行完成后会调用此方法 Log.d(TAG,"命令执行完毕"); } @Override public void commandOutput(int id, String line) { //命令执行的过程中会执行此方法,line参数可用于调试 Log.d(TAG,"命令执行中..."); } @Override public void commandTerminated(int id, String reason) { //命令被取消后的执行此方法 Log.d(TAG,"命令被取消"); } }; }