Android cell phone access to root and enable shutdown restart function

  • 2020-06-07 05:16:44
  • OfStack

This article describes the Android mobile phone access to root permissions and realize the shutdown and restart function, is a very common Android program design important function. I share it with you for your reference in the development of Android program.

The specific function code is as follows:


/*
 *  Execute the command 
 * @param command
 * 1 , get root permissions  "chmod 777 "+getPackageCodePath()
 * 2 And turn it off  reboot -p
 * 3 , restart  reboot
 */
public static boolean execCmd(String command) {
 Process process = null;
 DataOutputStream os = null;
 try {
 process = Runtime.getRuntime().exec("su");
 os = new DataOutputStream(process.getOutputStream());
 os.writeBytes(command+"\n");
 os.writeBytes("exit\n");
 os.flush();
 process.waitFor();
 } catch (Exception e) {
  return false;
 } finally {
  try {
  if (os != null) {
   os.close();
  }
  if(process != null) {
   process.destroy();
  }
  } catch (Exception e) {
  e.printStackTrace();
  }
 }
 return true;
}

It is hoped that the examples described in this article will be helpful to your Android programming.


Related articles: