I am developing a Ping application for Android 2.2.
I try my code and it works, but only in local IPs, that's my problem I want to do ping to external servers too.
Here is my 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 the following line at Manifest XML too:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Can anyone suggest me where I'm doing wrong?
this worked for me, no root,Android 6.0, Android Studio, Acatel U3:
In my case ping works from device but not from the emulator. I found this documentation: http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking
On the topic of "Local Networking Limitations" it says:
Further information: http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297
This is a simple ping I use in one of the projects:
Usage:
ping(new URL("https://www.google.com:443/"), this);
Result:
{"cnt":100,"dns":109,"host":"www.google.com","ip":"212.188.10.114","net":"WIFI"}
Use this Code: this method works on 4.3+ and also for below versions too.
Maybe ICMP packets are blocked by your (mobile) provider. If this code doesn't work on the emulator try to sniff via wireshark or any other sniffer and have a look whats up on the wire when you fire the isReachable() method.
You may also find some info in your device log.
I tried following code, which works for me.