• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

检查本地wifi上的互联网是否可用?

用户头像
it1352
帮助1

问题说明

我需要检查我的应用程序,我是否尝试过本地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

正确答案

#1

这是测试设备是否具有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.谷歌.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;

    }
}

这篇好文章是转载于:编程之路

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 编程之路
  • 本文地址: /reply/detail/tanhchabag
系列文章
更多 icon
同类精品
更多 icon
继续加载