本文主要是介绍Android 判断是否能真正上网(避免连入wifi无网的状态),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转自:http://blog.csdn.net/alien__chao/article/details/52184236
/***检测网络是否连接**@return*/privateboolean
isNetworkAvailable() {//得到网络连接信息ConnectivityManagermanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);//去进行判断网络是否连接if(manager.getActiveNetworkInfo() != null){returnmanager.getActiveNetworkInfo().isAvailable();}returnfalse;}
public static final boolean ping() {String result = null;try {String ip = "www.baidu.com";// ping 的地址,可以换成任何一种可靠的外网Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping网址3次// 读取ping的内容,可以不加InputStream input = p.getInputStream();BufferedReader in = new BufferedReader(new InputStreamReader(input));StringBuffer stringBuffer = new StringBuffer();String content = "";while ((content = in.readLine()) != null) {stringBuffer.append(content);}Log.d("------ping-----", "result content : " + stringBuffer.toString());// ping的状态int status = p.waitFor();if (status == 0) {result = "success";return true;} else {result = "failed";}} catch (IOException e) {result = "IOException";} catch (InterruptedException e) {result = "InterruptedException";} finally {Log.d("----result---", "result = " + result);}return false;}
Android学习交流群:523487222
(如果您觉得有用,欢迎加入,一起学习进步)
点击链接加入群【Android学习群】
这篇关于Android 判断是否能真正上网(避免连入wifi无网的状态)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!