从应用程序在 shell 中运行 ping 命令

发布于 2024-10-22 22:51:34 字数 693 浏览 2 评论 0原文

我发现源代码提供了从应用程序运行 shell 命令的机会,并且据我所知,返回一个包含执行命令结果的字符串:

http://code.google.com/p/netmeterled/source/browse /trunk/src/com/britoso/cpustatusled/utilclasses/ShellCommand.java?r=29

我尝试执行“ping -c 3 www.google.com”,但给定的方法返回“错误:null” ”。我执行命令的方式是:

ShellCommand cmd = new ShellCommand();
CommandResult r = cmd.sh.runWaitFor("ping -c 5 www.google.com");
String text;
if (!r.success()) {
    text = "Error:\n" + r.stderr;
}
else {
    text ="Ping test results:\n" + r.stdout;
}
log(text);

问题出在哪里?

I have found source code that gives an opportunity to run a shell command from the app, and, as I can understand, returns a string with the executed command result:

http://code.google.com/p/netmeterled/source/browse/trunk/src/com/britoso/cpustatusled/utilclasses/ShellCommand.java?r=29

I have tried to execute "ping -c 3 www.google.com", but the given method returns "Error: null". The way I execute the command is:

ShellCommand cmd = new ShellCommand();
CommandResult r = cmd.sh.runWaitFor("ping -c 5 www.google.com");
String text;
if (!r.success()) {
    text = "Error:\n" + r.stderr;
}
else {
    text ="Ping test results:\n" + r.stdout;
}
log(text);

Where is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀念你的温柔 2024-10-29 22:51:34

您现在可能已经解决了这个问题。但告诉你你不应该这样做对我来说似乎没有答案。显然,如果 ping 命令不存在,则无法调用它,并且可以处理错误。但如果是的话,哇!

这是一种方法。无论如何,可以在模拟器 2.1 上运行。不过,模拟器喜欢在互联网连接良好后启动。如果您启动模拟器并以某种方式更改连接,此后它可能无法工作。我不需要任何权限就可以在模拟器上执行此操作。

public class Rooted extends Activity {
    EditText body;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        body = (EditText)findViewById(R.id.bodyEditText);
        try {
        Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 betterpaving.com");
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        int i;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((i = reader.read(buffer)) > 0) output.append(buffer, 0, i);
        reader.close();
        body.append(output.toString()+"\n");
        } 
        catch (IOException e) {
            body.append("Error\n");
            e.printStackTrace();
            } 
        }//oncreate

    //TODO: Fill In Methods Etc.

    //"ping -c 1 betterpaving.com" try this sometime
}//class

无论如何,它与你的不同,需要放置在创建块之外的某个地方,但是......

You have probably solved this by now. But telling you you shouldn't do it seemed like no answer to me. Obviously if the ping command doesn't exist you can't call it and you can handle the error. But if it does, wow!

Here is a way to do it. Works on the emulator 2.1 anyway. The emulator likes to be started after you have a good Internet connection though. If you start the emulator and change your connection somehow, it may not work after that. I didn't need any permissions to do it either on the emulator.

public class Rooted extends Activity {
    EditText body;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        body = (EditText)findViewById(R.id.bodyEditText);
        try {
        Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 betterpaving.com");
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        int i;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((i = reader.read(buffer)) > 0) output.append(buffer, 0, i);
        reader.close();
        body.append(output.toString()+"\n");
        } 
        catch (IOException e) {
            body.append("Error\n");
            e.printStackTrace();
            } 
        }//oncreate

    //TODO: Fill In Methods Etc.

    //"ping -c 1 betterpaving.com" try this sometime
}//class

It's different from yours anyway and needs to be put somewhere besides the create block, but...

追星践月 2024-10-29 22:51:34

您的应用程序有互联网权限吗?该设备是否已连接到互联网? Logcat中有输出吗?

这个问题展示了一种可以用来从 Java 执行 ping 操作的方法,而无需运行 shell命令。

Do you have Internet permission for your app? And is the device connected to the Internet? Is there any output in the Logcat?

This question shows a one method you could use to ping from Java, without needing to run the shell command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文