我在一个可以从 android shell 发出 ping 或 traceroute 请求的应用程序上工作,我的应用程序读取结果并将其发布给用户。我的问题是我无法从命令中获得整个结果(消息)。例如,traceroute 在显示 HOPS 之前显示初始消息,并且 ping 仅适用于 1 个数据包。否则(对于许多数据包)我得到 ping 请求的初始部分而不是结果。我有一个根设备并安装了busybox,以便使用traceroute 命令。
代码如下
package com.example.commandshelltest;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv1;
String out = new String();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String result=execute_reboot();
TextView tv1=(TextView)findViewById(R.id.textView1);
//tv1.setText(Integer.toString(result.length())); // i can see that while increasing the number of icmp packets in ping the length stays the same
tv1.setText(result);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
String execute_reboot()
{
Process process1;
try {
int BUFF_LEN =1024;
process1=Runtime.getRuntime().exec("su");
DataOutputStream os =
new DataOutputStream(process1.getOutputStream());
//os.writeBytes("traceroute 8.8.8.8\n");
os.writeBytes("ping -c 1 8.8.8.8\n");
os.flush();
InputStream stdout = process1.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
while((read=stdout.read(buffer))>0)
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<BUFF_LEN){
//we have read everything
break;
}
}
os.writeBytes("exit\n");
os.flush();
process1.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return out;
}
}