Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

android - Why does ping works on some devices and not others?

I have the following code in my app...

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 8.8.8.8 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...