I have the following code in my app…
Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("/system/bin/ping -c 1 18.104.22.168"); return process.waitFor();
… and I’m seeing that it works (returns 0) on some devices (e.g. Motorola G) but doesn’t work (returns 2) on other devices (e.g. Galaxy S3). I’ve checked the Galaxy S3 device and it definitely has a “/system/bin/ping” file and of course I’ve made sure that it is indeed connected to the internet.
Anyone have any ideas why the command might not work on some devices… and what I can do to get it to work?
The way ping is implemented for every OEM (Original Equipment Manufacturer) may be different. This is not only limited to ping. For example, try running the linux du command on old Samsung devices (S3) and newer devices (Motorola G) on adb shell.
The S3 does support du, but it does not take some parameters such as -m (display in megabytes), while newer devices such as Moto G or even Samsung devices (past Galaxy S5) does support it. Try running ping -c 1 22.214.171.124 on adb shell within S3, and you’ll note that it’s not your Java code, it’s the OEM.
Again, even commands such as dumpsys have differently formatted output depending on Android Version and device. Because of this, it would be best if you can use a completely different method to try pinging the desired IP using APIs provided in Android instead of using a process with parameters that might be differently supported per OEM.
It can also be that the permissions are different per device.
When you run a process within your app, you only are granting the app’s limited permissions to the process. So if ping requires root level permission (on the specific device), that may also be the reason why it does not work. It is ultimately up to the OEM of how they customized Android.
Source: I work at an OEM.