admin 管理员组文章数量: 887021
2024年1月18日发(作者:python语言程序设计教程赵璐)
Android下执行脚本
/**
* Internal thread used to execute scripts (as root or not).
*/
private static final class ScriptRunner extends Thread {
private final File file;
private final String script;
private final StringBuilder res;
private final boolean asroot;
public int exitcode = -1;
private Process exec;
/**
* Creates a new script runner.
* @param file temporary script file
* @param script script to run
* @param res response output
* @param asroot if true, executes the script as root
*/
public ScriptRunner(File file, String script, StringBuilder res, boolean asroot) {
= file;
= script;
= res;
= asroot;
}
@Override
public void run() {
try {
NewFile();
final String abspath = olutePath();
// make sure we have execution permission on the script file
time().exec("chmod 777 "+abspath).waitFor();
// Write the script to be executed
final OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file));
("#!/system/bin/shn");
(script);
if (!th("n")) ("n");
("exitn");
();
();
if () {
// Create the "su" request to run the script
exec = time().exec("su -c "+abspath);
} else {
// Create the "sh" request to run the script
exec = time().exec("sh "+abspath);
}
InputStreamReader r = new InputStreamReader(utStream());
final char buf[] = new char[1024];
int read = 0;
// Consume the "stdout"
while ((read=(buf)) != -1) {
if (res != null) (buf, 0, read);
}
// Consume the "stderr"
r = new InputStreamReader(orStream());
read=0;
while ((read=(buf)) != -1) {
if (res != null) (buf, 0, read);
}
// get the process exit code
if (exec != null) de = r();
} catch (InterruptedException ex) {
if (res != null) ("nOperation timed-out");
} catch (Exception ex) {
if (res != null) ("n" + ex);
} finally {
destroy();
}
}
/**
* Destroy this script runner
*/
public synchronized void destroy() {
if (exec != null) y();
exec = null;
}
}
/**
* Runs a script, wither as root or as a regular user (multiple commands separated by
"n").
* @param ctx mandatory context
* @param script the script to be executed
* @param res the script output response (stdout + stderr)
* @param timeout timeout in milliseconds (-1 for none)
* @return the script exit code
*/
public static int runScript(Context ctx, String script, StringBuilder res, long timeout,
boolean asroot) {
final File file = new File(heDir(), SCRIPT_FILE);
final ScriptRunner runner = new ScriptRunner(file, script, res, asroot);
();
try {
if (timeout > 0) {
(timeout);
} else {
();
}
if (e()) {
// Timed-out
upt();
(150);
y();
(50);
}
} catch (InterruptedException ex) {}
return de;
}
/**
* Runs a script as root (multiple commands separated by "n").
* @param ctx mandatory context
* @param script the script to be executed
* @param res the script output response (stdout + stderr)
* @param timeout timeout in milliseconds (-1 for none)
* @return the script exit code
*/
public static int runScriptAsRoot(Context ctx, String script, StringBuilder res, long timeout)
{
return runScript(ctx, script, res, timeout, true);
}
private static boolean hasroot = false;
/**
* Check if we have root access
* @param ctx mandatory context
* @return boolean true if we have root
*/
public static boolean hasRootAccess(Context ctx) {
if (hasroot) return true;
final StringBuilder res = new StringBuilder();
try {
// Run an empty script just to check root access
if (runScriptAsRoot(ctx, "exit 0", res) == 0) {
hasroot = true;
return true;
}
} catch (Exception e) {
}
return false;
}
版权声明:本文标题:Android下执行脚本 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1705543561h489241.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论