Code:
1 PublicString Run (String _strsip)throwsException2 {3String Strrst = "";4 Try5 {6String str = "Ping-c 1" +_strsip;7Runtime runtime =runtime.getruntime ();8Process Process =runtime.exec (str);9 Ten //wait for the process to finish executing One if(Process.waitfor ()! = 0) A { - //If the process does not run a result of 0, it indicates that the process exited with an error - //get error output from process instance theInputStream Streamerr =Process.geterrorstream (); - //Do something - -Strrst =inputstream2string (streamerr); + if(Streamerr! =NULL) - streamerr.close (); + A if(Strrst.isempty ()) at { -InputStream streamin =Process.getinputstream (); -Strrst =inputstream2string (streamin); - if(StreamIn! =NULL) - streamin.close (); - in if(Strrst.isempty ()) -Strrst = "failed:ping Failed."; to } + returnStrrst; - } the *InputStream streamin =Process.getinputstream (); $Strrst =inputstream2string (streamin);Panax Notoginseng if(StreamIn! =NULL) - streamin.close (); the + if(Strrst.isempty ()) AStrrst = "Succeed:ping failed."; the } + Catch(Exception ex) - { $ //ex.printstacktrace (); $StringWriter SW =NewStringWriter (); -PrintWriter PW =NewPrintWriter (SW); - ex.printstacktrace (PW); theStrrst = "ERR:" +sw.tostring (); - }Wuyi returnStrrst; the}
Android ping command-Runtime