2022-05-27 Android Android getRuntime()、exec 执行命令(linux 下的bin文件等)并解得到返回值、执行ping命令
一、形式一:只执行命令不获取返回值。public static String execute_command(String cmd) {try {//String keyCommand = "setprop " + propName;Runtime runtime = Runtime.getRuntime();Process proc = runtime.exec(cmd);}..
·
一、形式一:只执行命令不获取返回值。
public static String execute_command(String cmd) {
try {
//String keyCommand = "setprop " + propName;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
return cmd;
}
二、形式二:执行命令并且获取返回值。
private String playRunTime(String cmd) throws Exception
{
// String cmd = "adb version";
String ret = null;
Process p = Runtime.getRuntime().exec(cmd);
InputStream is = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line; while ((line = reader.readLine()) != null)
{
//tv_result.append(line + "");
ret = line;
System.out.println(TAG+" "+line);
}
p.waitFor();
is.close();
reader.close();
p.destroy();
return ret;
}
三、执行ping的需要下面的形式
public boolean execCommand(String cmd){
Process process = null;
DataOutputStream os = null;
try{
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd+"\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
String line;
Log.i(TAG, "execCommand: " + "BufferedReader");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
Log.i(TAG, "bufferedReader: " + "BufferedReader readLine");
while ((line = bufferedReader.readLine()) != null) {
Log.i(TAG, "bufferedReader read: " + line);
}
Log.i(TAG, "waitFor");
process.waitFor();
Log.i(TAG, "waitFor END");
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
四、实例测试
1、java源码
package com.giada.sn_writer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
private Button m_Button;
private EditText m_EditText;
private String TAG = "EXECUTECOMMAND";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_Button=(Button)findViewById(R.id.button);
m_EditText = (EditText) findViewById(R.id.textView);
m_Button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
String str = m_EditText.getText().toString();
// execute_command("sn_writer write-key "+ str);
// playRunTime("sn_writer write-key "+ str);
try {
playRunTime("ls");
playRunTime("cat /sys/class/power_supply/battery/voltage_now");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static String execute_command(String cmd) {
try {
//String keyCommand = "setprop " + propName;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
return cmd;
}
private String playRunTime(String cmd) throws Exception
{
// String cmd = "adb version";
String ret = null;
Process p = Runtime.getRuntime().exec(cmd);
InputStream is = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line; while ((line = reader.readLine()) != null)
{
//tv_result.append(line + "");
ret = line;
System.out.println(TAG+" "+line);
}
p.waitFor();
is.close();
reader.close();
p.destroy();
return ret;
}
}
2、执行效果
更多推荐
已为社区贡献12条内容
所有评论(0)