本文的Demo源代码已在新浪微盘发布:Hi,推荐文件给你 "ShellProject.zip"http://vdisk.weibo.com/s/mg606
新浪分享也已上传,http://ishare.iask.sina.com.cn/f/35304183.html 。
--------------------------------------------------------------------------
关键代码:
public static void doCmds(List cmds) throws Exception {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
}
eg.在button的click事件调用:
this.btn=(Button) super.findViewById(R.id.btn);
this.btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
List cmds=new ArrayList();
cmds.add("reboot");
try {
MainActivity.doCmds(cmds);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
点击按钮后,系统会重启哦。
--------------------------
前提:手机root完,允许root权限。