2

I am making an android application which can get ping response time from android application. I have already done it with windows environment but when i take it onto android device, device can't send any response. How can i solve this problem.?

Thanks in advance. here is my code

                 String ip="www.google.com"; String pingResult="  ";
                String pingCmd="ping"+ip;

                Runtime r=Runtime.getRuntime();
                Process p=r.exec(pingCmd);
                BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
                String inputLine;
                Toast.makeText(getApplicationContext(), "Going loop", 1).show();
                while((inputLine=in.readLine())!=null)
                {
                    pingResult=inputLine;
                }
                Toast.makeText(getApplicationContext(),pingResult, 1).show();
                in.close();
  • Look at this question, http://stackoverflow.com/questions/2786720/android-service-ping-url?rq=1 – Austin Sep 30 '12 at 14:14

1 Answers1

1

Your will work fine if you change the line:

Process p=r.exec(pingCmd); 

to:

Process p=r.exec(new String[] {"ping", "-c 4", "www.google.pt"}); 

The reason is:

-Android exec command expects a String[] instead of String.

-The -c parameter is required to limit the number of pings to 4 (to replicate the windows behaviour). Otherwise it will ping forever.

Finally, this only works on a real device. To have it working in the emulator, you would need to configure adb to redirect ping replays to the emulator.

Good luck.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
Luis
  • 11,978
  • 3
  • 27
  • 35
  • How do i `configure adb to redirect for ping reply to the emulator` .- @Luis – Mahmudul Hassan Shovon Sep 30 '12 at 17:36
  • I had a quick look on adb documentation and it looks like you can't do it. You can only do it for TCP and UDP protocols. For ping it would require also the ICMP protocol. Just for your info, tcp and udp can be redirected using the command "adb -e forward". – Luis Sep 30 '12 at 17:53