点击(此处)折叠或打开
-
-
import java.io.DataOutputStream;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.io.InputStreamReader;
-
-
import android.app.Activity;
-
-
public class SystemManager extends Activity {
-
-
public static Process RootCommand(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) {
-
DnsServerActivity.displayer("err:" + e.getMessage() + "\r\n");
-
return null;
-
} finally {
-
try {
-
if (os != null) {
-
os.close();
-
}
-
//process.destroy();
-
} catch (Exception e) {
-
DnsServerActivity.displayer("err:" + e.getMessage() + "\r\n");
-
return null;
-
}
-
}
-
DnsServerActivity.displayer("Command finished" + "\r\n");
-
-
return process;
-
}
-
-
public static String readStreamString(InputStream is) throws IOException {
-
StringBuffer localStringBuffer = new StringBuffer();
-
InputStreamReader localInputStreamReader = new InputStreamReader(is);
-
char[] arrayOfChar = new char[65536];
-
int res = 0;
-
while (res >= 0) {
-
res = localInputStreamReader.read(arrayOfChar);
-
-
if (res >= 0) {
-
localStringBuffer.append(arrayOfChar, 0, res);
-
}
-
-
}
-
-
return localStringBuffer.toString();
-
}
-
-
public static String execOnce(String cmd) throws IOException {
-
-
java.lang.Process localProcess = Runtime.getRuntime().exec(cmd);
-
-
// String[] args = cmd.split("\\s+");;
-
// java.lang.Process localProcess = Runtime.getRuntime().exec(args);
-
-
// java.lang.Process localProcess = Runtime.getRuntime().exec("sh");
-
// DataOutputStream os = new
-
// DataOutputStream(localProcess.getOutputStream());
-
// os.writeBytes(cmd + "\n");
-
// os.writeBytes("exit" + "\n");
-
// os.flush();
-
-
return readStreamString(localProcess.getInputStream());
-
-
}
-
-
public static int getProcessPid(String processName) {
-
try {
-
-
String output = execOnce("ps");
-
String[] lines = output.split("\n");
-
-
for (int i = 0; i < lines.length; i++) {
-
// print("line=" + lines[i]);
-
String[] attr = lines[i].split("[\\s]+");
-
-
// print("attr.length =" + attr.length );
-
if (attr.length != 9)
-
continue;
-
-
String name = attr[8];
-
int pid = Integer.parseInt(attr[1]);
-
// print("pid=" + pid + ", name=" + name);
-
if (name.equals(processName))
-
return pid;
-
}
-
-
return -1;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return -1;
-
}
-
}
-
- }
2. 通过简单的页面使用上述的java 类, 实现开控制相关命令行程序的启动,同时打印相关命令行的输出log:
点击(此处)折叠或打开
-
public class DnsServerActivity extends Activity {
-
/** Called when the activity is first created. */
-
-
public static TextView msgEditText;
-
public static ScrollView msgScrollView;
-
public Process process = null;
-
-
private static final String TAG = "DnsServerActivity";
-
public boolean stop_flag = false;
-
public static boolean logdisplay = true;
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
msgEditText = (TextView) findViewById(R.id.dns_textview);
-
msgScrollView = (ScrollView) findViewById(R.id.dns_msg);
-
-
CheckBox cb = (CheckBox) findViewById(R.id.dns_cb);
-
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
-
-
@Override
-
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
-
if (logdisplay == true) {
-
logdisplay = false;
-
} else {
-
logdisplay = true;
-
}
-
-
displayer("\r\ndisplay log: " + logdisplay + "\r\n");
-
}
-
});
-
-
mThread.start();
-
-
}
-
-
public static Handler handler = new Handler();
-
-
public static void displayer(final String content) {
-
handler.post(new Runnable() {
-
@Override
-
public void run() {
-
// msgEditText.append("\r\n");
-
-
if (logdisplay == false) {
-
return;
-
}
-
-
msgEditText.append(content);
-
// Toast.makeText(getApplicationContext(), content,
-
// Toast.LENGTH_LONG).show();
-
-
handler.post(scrollToBottom);
-
}
-
});
-
}
-
-
@Override
-
protected void onDestroy() {
-
// TODO Auto-generated method stub
-
stop_flag = true;
-
-
displayer("dns is onDestroy ... \r\n");
-
super.onDestroy();
-
}
-
-
public static Runnable scrollToBottom = new Runnable() {
-
@Override
-
public void run() {
-
int off = msgEditText.getMeasuredHeight()
-
- msgScrollView.getHeight();
-
if (off > 0) {
-
msgScrollView.scrollTo(0, off);
-
}
-
}
-
};
-
-
private Thread mThread = new Thread() {
-
-
Process process = null;
-
int procesId = -1;
-
-
public void run() {
-
displayer("\r\nthread is runing in \r\n");
-
-
String dir = getApplicationContext().getFilesDir().getParent();
- String cmd = "." + dir + "/lib/cmdline";
-
String pname = "." + dir + "/lib/cmdline";
-
-
if ((procesId = SystemManager.getProcessPid(pname)) > 0) {
-
displayer("dns has already run with id --: " + procesId
-
+ "\r\n");
-
process = SystemManager.RootCommand("kill " + procesId);
-
process.destroy();
-
process = null;
-
displayer("pthread " + procesId + " has been killed\r\n");
-
}
-
-
try {
-
Thread.sleep(5000);
-
} catch (InterruptedException e2) {
-
// TODO Auto-generated catch block
-
e2.printStackTrace();
-
}
-
-
process = SystemManager.RootCommand(cmd + "&");
-
displayer("\r\n\r\nstart cmd: \n" + cmd + "&" + "\r\n\r\n");
-
-
InputStream in = process.getInputStream();
-
InputStream errIn = process.getErrorStream();
-
-
LineNumberReader input = new LineNumberReader(
-
new InputStreamReader(in));
-
LineNumberReader errorInput = new LineNumberReader(
-
new InputStreamReader(errIn));
-
String line = null;
-
-
int errtag = 0;
-
int inputag = 0;
-
-
while (!stop_flag) {
-
try {
-
displayer("\r\ndns sever is runnig --- " + "\r\n");
-
-
if (input.ready() && (line = input.readLine()) != null) {
-
displayer(line);
-
inputag = 1;
-
} else {
-
inputag = 0;
-
}
-
-
if (errorInput.ready()
-
&& (line = errorInput.readLine()) != null) {
-
displayer(line);
-
errtag = 1;
-
} else {
-
errtag = 0;
-
}
-
-
if (errtag == 0 && inputag == 0) {
-
Thread.sleep(500);
-
displayer("no log in ...\r\n");
-
}
-
-
} catch (IOException e1) {
-
// TODO Auto-generated catch block
-
e1.printStackTrace();
-
break;
-
} catch (InterruptedException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
-
}
-
-
process.destroy();
-
-
displayer("dns sever is quiting" + "\r\n");
-
-
}
-
};
- }