安卓......如何找出我是否在一个wifi互联网上?[英] Android ... how to find out if I'm on a wifi internet?

本文是小编为大家收集整理的关于安卓......如何找出我是否在一个wifi互联网上?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在我的应用程序中,我只需要知道设备是否连接到WiFi网络.我认为这个功能适用于仿真器,但不是真实的设备.

public static boolean wifiInternet(Context c)
{
    try
    {
        ConnectivityManager connectivityManager = (ConnectivityManager)c.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = connectivityManager.getActiveNetworkInfo();  // CRASHES HERE
        String name = ni.getTypeName(); 
        if(name.equals("WIFI"))
            return true;
        else
            return false;
    }
    catch(Exception e)
    {
        return false;
    }
}

以及我在这里使用哪种情况? getaplicationContext()或getBaseContext()或者我只是把'这个'(我呼吁服务中的函数).

推荐答案

试用:

  public boolean isUsingWiFi() {
    ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiInfo = connectivity
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifiInfo != null && wifiInfo.getState() == NetworkInfo.State.CONNECTED
            || wifiInfo.getState() == NetworkInfo.State.CONNECTING) {
        return true;
    }

    return false;
}

您还可以使用相同的移动类型:

  public boolean isUsingMobileData() {
    ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo mobileInfo = connectivity
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (mobileInfo != null && mobileInfo.getState() == NetworkInfo.State.CONNECTED
            || mobileInfo.getState() == NetworkInfo.State.CONNECTING) {
        return true;
    }

    return false;
}

本文地址:https://www.itbaoku.cn/post/102699.html

问题描述

In my application I just need to know if the device is connected to wifi network or not. I think this function works on emulator but not on real device.

public static boolean wifiInternet(Context c)
{
    try
    {
        ConnectivityManager connectivityManager = (ConnectivityManager)c.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = connectivityManager.getActiveNetworkInfo();  // CRASHES HERE
        String name = ni.getTypeName(); 
        if(name.equals("WIFI"))
            return true;
        else
            return false;
    }
    catch(Exception e)
    {
        return false;
    }
}

And which context do I use here? getAplicationContext() or getBaseContext() or do I just put 'this' (I'm calling the function from a Service).

推荐答案

Try this:

  public boolean isUsingWiFi() {
    ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiInfo = connectivity
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifiInfo != null && wifiInfo.getState() == NetworkInfo.State.CONNECTED
            || wifiInfo.getState() == NetworkInfo.State.CONNECTING) {
        return true;
    }

    return false;
}

You can also use the same for the mobile type:

  public boolean isUsingMobileData() {
    ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo mobileInfo = connectivity
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (mobileInfo != null && mobileInfo.getState() == NetworkInfo.State.CONNECTED
            || mobileInfo.getState() == NetworkInfo.State.CONNECTING) {
        return true;
    }

    return false;
}