Android:Ping命令测试网络

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
 
public class MainActivity extends Activity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		new NetPing().execute();
	}
	
	public String Ping(String str) {
		String resault = "";
			Process p;
			try {
				//ping -c 3 -w 100  中  ,-c 是指ping的次数 3是指ping 3次 ,-w 100  以秒为单位指定超时间隔,是指超时时间为100秒 
				p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + str);
				int status = p.waitFor();
 
				InputStream input = p.getInputStream();
				BufferedReader in = new BufferedReader(new InputStreamReader(input));
			    StringBuffer buffer = new StringBuffer();
			    String line = "";
			    while ((line = in.readLine()) != null){
			      buffer.append(line);
			    }
			    System.out.println("Return ============" + buffer.toString());
 
				if (status == 0) {
					resault = "success";
				} else {
					resault = "faild";
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
 
		return resault;
	}
	
	private class NetPing extends AsyncTask<String, String, String> {
 
		@Override
		protected String doInBackground(String... params) {
			String s = "";
			s = Ping("www.baidu.com");
			Log.i("ping", s);
			return s;
		}
	}
		
 
 
}

!!!别忘了加访问网络权限!!!
下面是运行截图:
在这里插入图片描述

如果对 运行结果中的 PING www.a.shifen.com (61.135.169.125) 感到怀疑,那么可以试一下 61.135.169.125 就是百度的网址。

suceess则表示连接成功

如果当前没有连接网络,return 会是 空的 ,结果会是 faild。

如果ping的网址是不存在的,而且还连接着网络,那么程序就会 停在

int status = p.waitFor();

这里,不再向下运行。

Ping远程IP 

这一命令可以检测本机能否正常访问Internet。比如本地电信运营商的IP地址为:202.101.224.69。在MS-DOS方式下执行命令:Ping 202.101.224.69,如果屏幕显示:

C:\Documents and Settings\Administrator>ping 202.101.224.69

Pinging 202.101.224.69 with 32 bytes of data:

Reply from 202.101.224.69: bytes=32 time=2ms TTL=250

Reply from 202.101.224.69: bytes=32 time=2ms TTL=250

Reply from 202.101.224.69: bytes=32 time=3ms TTL=250

Reply from 202.101.224.69: bytes=32 time=2ms TTL=250

Ping statistics for 202.101.224.69:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds:

Minimum = 2ms, Maximum = 3ms, Average = 2ms

则表明运行正常,能够正常接入互联网。反之,则表明主机文件(windows/host)存在问题。

如果想多了解更多关于Ping的知识,http://wenku.baidu.com/view/2042e1630b1c59eef8c7b46a.html 这里可以看一下。

上面的代码是在别人那里下载,自己稍微改动的。下载地址为 http://download.csdn.net/detail/allvenacm/4845468

不过不好意思,我给那位的评论写错了。
————————————————
版权声明:本文为CSDN博主「VanishMagic」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012251822/article/details/12647211
其它参考资料:https://blog.csdn.net/android_cai_niao/article/details/118022111

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值