How to Ping External IP from Java Android -
i developing ping application android 2.2.
i try code , works, in local ips, that's problem want ping external servers too.
here code:
private onclicklistener milistener = new onclicklistener() { public void onclick(view v) { textview info = (textview) findviewbyid(r.id.info); edittext edit = (edittext) findviewbyid(r.id.edit); editable host = edit.gettext(); inetaddress in; in = null; // definimos la ip de la cual haremos el ping try { in = inetaddress.getbyname(host.tostring()); } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } // definimos un tiempo en el cual ha de responder try { if (in.isreachable(5000)) { info.settext("responde ok"); } else { info.settext("no responde: time out"); } } catch (ioexception e) { // todo auto-generated catch block info.settext(e.tostring()); } } };
ping 127.0.0.1 -> ok
ping 8.8.8.8 (google dns) -> time out
i put following line @ manifest xml too:
<uses-permission android:name="android.permission.internet"></uses-permission>
can suggest me i'm doing wrong?
i tried following code, works me.
private boolean executecommand(){ system.out.println("executecommand"); runtime runtime = runtime.getruntime(); try { process mipaddrprocess = runtime.exec("/system/bin/ping -c 1 8.8.8.8"); int mexitvalue = mipaddrprocess.waitfor(); system.out.println(" mexitvalue "+mexitvalue); if(mexitvalue==0){ return true; }else{ return false; } } catch (interruptedexception ignore) { ignore.printstacktrace(); system.out.println(" exception:"+ignore); } catch (ioexception e) { e.printstacktrace(); system.out.println(" exception:"+e); } return false; }
Comments
Post a Comment