检查是否WiFi是连接返回false,即使它是连接 [英] Checking if wifi is connected returning false even though it is connected

查看:902
本文介绍了检查是否WiFi是连接返回false,即使它是连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android项目,我试图检查Android设备连接到无线网络,但是,我的功能总是返回假的,即使我的设备成功连接到无线网络。

在构造函数类我通过调用方法的情况下,并在构造函数中创建的实例 ConnnectivityManager

下面是构造

 公共NetworkManagement(上下文的背景下)
{
    this.context =背景;
    connectivityManager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
}

下面是我有问题我的功能

 公共布尔isConnectedToWifi()
{
    的NetworkInfo NETWORKINFO = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    如果(networkInfo.isConnected())
    {
        返回true;
    }
    其他
    {
        返回false;
    }
}

虽然我的设备连接到WiFi网络,我可以成功连接到互联网,上面的功能将始终返回false,但我不明白为什么。

感谢您的帮助,您可以提供。


解决方案

 公共类NetworkManagement
{
    公共上下文的背景下;    公共NetworkManagement(上下文的背景下)
    {
        this.context =背景;
    }    公共布尔isConnectedToWifi()
    {
        ConnectivityManager connectivityManager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        的NetworkInfo NETWORKINFO = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        如果(networkInfo.isConnected())
        {
            返回true;
        }
        其他
        {
            返回false;
        }
    }}

和电话

  //使用环境测试= getApplicationContext();
NetworkManagement一个=新NetworkManagement(上下文);
布尔Z = a.isConnectedToWifi();

z来要妥善归还

编辑:它仍然不能正常工作,然后我把它。如果仍然没有这将是在上下文中出现故障

I am working on an android project and I am trying to check if an android device is connected to Wifi, however, my function is always returning false, even though my device is successfully connected to Wifi.

In the constructor for the class I pass the context of the calling method, and within the constructor I create an instance of the ConnnectivityManager

Below is the constructor

public NetworkManagement(Context context)
{
    this.context = context;
    connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
}

Below is my function that I am having the problem with

public boolean isConnectedToWifi()
{
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (networkInfo.isConnected())
    {
        return true;
    }
    else
    {
        return false;
    }
}

Even though my device is connected to a wifi network and I can successfully connect to the internet, the function above will always return false but I do not understand why.

Thanks for any help you can provide.

解决方案

public class NetworkManagement
{
    public Context context;

    public NetworkManagement(Context context)
    {
        this.context = context;     
    }

    public boolean isConnectedToWifi()
    {
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo.isConnected())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}

and call

//tested with context=getApplicationContext();
NetworkManagement a = new NetworkManagement(context);
Boolean z = a.isConnectedToWifi();

z should return it properly

Edit : It still isn't working properly then i take it. If it still isn't it would be a fault in the context

这篇关于检查是否WiFi是连接返回false,即使它是连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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