如何检查本地wifi上的互联网是否可用? [英] how to check that internet on local wifi is available or not?

查看:111
本文介绍了如何检查本地wifi上的互联网是否可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查我的应用程序,我是否尝试过本地wifi上的互联网可用

I need to check oin my app that internet on local wifi is available or not , i tried

requestRouteToHost 但是它总是返回false,所以请帮忙

requestRouteToHost but it always returning false , so please help

推荐答案

这是测试设备是否具有INTERNET连接周期的最佳方法.

Here is the best way to test if the device has INTERNET connection period.

public static boolean hasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
    try {
        HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
        urlc.setRequestProperty("User-Agent", "Test");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1500); 
        urlc.connect();
        return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error checking internet connection", e);
    }
} else {
    Log.d(LOG_TAG, "No network available!");
}
return false;

}

这是一种检查它是否具有wifi连接或数据连接的方法.

Here's a way to check if it has wifi connection or data connection.

  private boolean checkInternetConnection()
  {

    ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);

    // ARE WE CONNECTED TO THE NET

    if (conMgr.getActiveNetworkInfo() != null

            && conMgr.getActiveNetworkInfo().isAvailable()

            && conMgr.getActiveNetworkInfo().isConnected()) 
    {

    return true;

    }
    else 
    {
    return false;

    }
}

这篇关于如何检查本地wifi上的互联网是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆