How do I see if Wi-Fi is connected on Android?

前端 未结 22 2613
挽巷
挽巷 2020-11-22 05:56

I don\'t want my user to even try downloading something unless they have Wi-Fi connected. However, I can only seem to be able to tell if Wi-Fi is enabled, but they could sti

相关标签:
22条回答
  • 2020-11-22 06:16

    This works for me:

        ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    
        // Mobile
        State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    
        // Wi-Fi
        State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    
        // And then use it like this:
    
        if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
        {
            Toast.makeText(Wifi_Gprs.this,"Mobile is Enabled :) ....",Toast.LENGTH_LONG).show();
        }
        else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
        {
            Toast.makeText(Wifi_Gprs.this,"Wifi is Enabled  :) ....",Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(Wifi_Gprs.this,"No Wifi or Gprs Enabled :( ....",Toast.LENGTH_LONG).show();
        }
    

    And add this permission:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    0 讨论(0)
  • 2020-11-22 06:17

    This is an easier solution. See Stack Overflow question Checking Wi-Fi enabled or not on Android.

    P.S. Do not forget to add the code to the manifest.xml file to allow permission. As shown below.

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
    </uses-permission>
    
    0 讨论(0)
  • 2020-11-22 06:18

    The NetworkInfo class is deprecated as of API level 29, along with the related access methods like ConnectivityManager#getNetworkInfo() and ConnectivityManager#getActiveNetworkInfo().

    The documentation now suggests people to use the ConnectivityManager.NetworkCallback API for asynchronized callback monitoring, or use ConnectivityManager#getNetworkCapabilities or ConnectivityManager#getLinkProperties for synchronized access of network information

    Callers should instead use the ConnectivityManager.NetworkCallback API to learn about connectivity changes, or switch to use ConnectivityManager#getNetworkCapabilities or ConnectivityManager#getLinkProperties to get information synchronously.


    To check if WiFi is connected, here's the code that I use:

    Kotlin:

    val connMgr = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
    connMgr?: return false
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        val network: Network = connMgr.activeNetwork ?: return false
        val capabilities = connMgr.getNetworkCapabilities(network)
        return capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
    } else {
        val networkInfo = connMgr.activeNetworkInfo ?: return false
        return networkInfo.isConnected && networkInfo.type == ConnectivityManager.TYPE_WIFI
    }
    

    Java:

    ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connMgr == null) {
        return false;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Network network = connMgr.getActiveNetwork();
        if (network == null) return false;
        NetworkCapabilities capabilities = connMgr.getNetworkCapabilities(network);
        return capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
    } else {
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        return networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
    }
    

    Remember to also add permission ACCESS_NETWORK_STATE to your Manifest file.

    0 讨论(0)
  • 2020-11-22 06:20
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    boolean is3g = manager.getNetworkInfo(
                      ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
    boolean isWifi = manager.getNetworkInfo(
                        ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
    
    Log.v("", is3g + " ConnectivityManager Test " + isWifi);
    if (!is3g && !isWifi) {
        Toast.makeText(
            getApplicationContext(),
            "Please make sure, your network connection is ON ",
            Toast.LENGTH_LONG).show();
    }
    else {
        // Put your function() to go further;
    }
    
    0 讨论(0)
  • 2020-11-22 06:20

    Try

    wifiManager.getConnectionInfo().getIpAddress()
    

    This returns 0 until the device has a usable connection (on my machine, a Samsung SM-T280, Android 5.1.1).

    0 讨论(0)
  • 2020-11-22 06:21
    val wifi = context!!.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager?        
             if (wifi!!.isWifiEnabled) 
                  //do action here
        
             else 
                 //do action here
        
            
                        
    
    0 讨论(0)
提交回复
热议问题