java process 重启_JAVA Process启动sh 后的问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import android.annotation.SuppressLint;

import android.os.*;

import java.io.*;

import java.lang.Process;

public class RootShellTask extends Thread

{

@SuppressLint("HandlerLeak")

Handler h = new Handler(){

@Override

public void handleMessage(Message msg)

{

// TODO: Implement this method

super.handleMessage(msg);

Bundle b = msg.getData();

int flag = b.getInt("flag");

if (flag == FLAG_START)

{

boolean root = b.getBoolean("root");

String command = b.getString("data");

RootShellTask.this.call.onStart(command, root);

}

else if (flag == FLAG_RUN)

{

String output = b.getString("data");

RootShellTask.this.call.onRun(output);

}

else if (flag == FLAG_FINISH)

{

int code = b.getInt("data");

RootShellTask.this.call.onFinish(code);

}

}

};

public void run()

{

Message m;

Bundle b;

try

{

String shell = root ? "su" : "sh";

process = Runtime.getRuntime().exec(shell, envp);

os = new DataOutputStream(process.getOutputStream());

if (call != null)

{

m = new Message();

b = new Bundle();

b.putInt("flag", FLAG_START);

b.putString("data", command);

b.putBoolean("root", root);

m.setData(b);

h.sendMessage(m);

}

os.write((command).getBytes());

os.write(("\n").getBytes());

os.write(("exit $?\n").getBytes());

os.flush();

if (call != null)

{

stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));

stderr = new BufferedReader(new InputStreamReader(process.getErrorStream()));

String out = "";

String err = "";

while ((out = stdout.readLine()) != null | (err = stderr.readLine()) != null)

{

StringBuffer s = new StringBuffer();

if (out != null)

{

s.append(out + "\n");

}

if (err != null)

{

s.append(err + "\n");

}

m = new Message();

b = new Bundle();

b.putInt("flag", FLAG_RUN);

b.putString("data", s.toString());

m.setData(b);

h.sendMessage(m);

}

}

int code = process.waitFor();

if (call != null)

{

m = new Message();

b = new Bundle();

b.putInt("flag", FLAG_FINISH);

b.putInt("data", code);

m.setData(b);

h.sendMessage(m);

}

}

catch (Exception e)

{

e.printStackTrace();

}

finally

{

try

{

if (os != null)

{

os.close();

}

if (stderr != null)

{

stderr.close();

}

if (stdout != null)

{

stdout.close();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

int FLAG_FINISH = 0;

int FLAG_RUN = 1;

int FLAG_START = 2;

private Process process;

private DataOutputStream os;

private BufferedReader stderr;

private BufferedReader stdout;

private RootShellCallback call;

private String command;

private String[] envp;

private boolean root;

public RootShellTask(String command)

{

this(command, true);

}

public RootShellTask(String command, RootShellCallback call)

{

this(command, true, call);

}

public RootShellTask(String command, boolean root)

{

this(command, root, null);

}

public RootShellTask(String command, boolean root, RootShellCallback call)

{

this(command, root, call, null);

}

public RootShellTask(String command, boolean root, RootShellCallback call, String[] envp)

{

this.call = call;

this.envp = envp;

this.command = command;

this.root = root;

}

public void setProcess(Process process)

{

this.process = process;

}

public Process getProcess()

{

return process;

}

}

interface RootShellCallback

{

void onStart(String command, boolean root);

void onRun(String output);

void onFinish(int exitCode);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值