Listening to WIFI Status

I want the listener to listen to the status of the wireless network, can someone help me with my code.

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

...

 TelephonyManager wTelephonyManager;

...

     wTelephonyManager=(TelephonyManager)getSystemService(Context.WIFI_SERVICE);
    wTelephonyManager.listen(new PhoneL(),PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

// here is the case that I use so that I want to listen to the Wi-Fi change and the above code is in onCreate {}

class PhoneL extends PhoneStateListener

{   






    public void onWifiStateChanged(int state, String nesto)
    {
         mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //mWifi.isConnectedOrConnecting()
             if(mWifi.isConnectedOrConnecting())
             {
                 Toast.makeText(WifiActivity.this,"Ima WIFI",Toast.LENGTH_LONG).show();     
             }
             else
             {
                 Toast.makeText(WifiActivity.this,"! NEMA WIFI",Toast.LENGTH_LONG).show(); 
             }


    }

}

// Can someone help me create a listener that will listen to wifi status and check if Wi-Fi is connected or connect if I do not want to enable data packet traffic via 3g / 4g

+5
source share
3 answers

Data Traffic, WiFi , .
WiFi, , WiFi 3G, , , , .
, WiFi, , - .
- , , ( ) Wi-Fi-, , WiFi, (, ) Wi-Fi - (), . Android.

-2

, .

onCreate (..)

this.registerReceiver(mWifiStateChangedReceiver,new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));

, , 'mWifiStateChangedReceiver'

private BroadcastReceiver mWifiStateChangedReceiver = new BroadcastReceiver()
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        // TODO Auto-generated method stub

        int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);

        switch (extraWifiState)
        {
        case WifiManager.WIFI_STATE_DISABLED:
        case WifiManager.WIFI_STATE_DISABLING:
            enableUI(false);
            break;
        case WifiManager.WIFI_STATE_ENABLED:
            ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
            while(conMan.getActiveNetworkInfo() == null || conMan.getActiveNetworkInfo().getState() != NetworkInfo.State.CONNECTED)
            {
                try
                {
                    Thread.sleep(500);
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            update();
            enableUI(true);
            break;
        case WifiManager.WIFI_STATE_ENABLING:
            break;
        case WifiManager.WIFI_STATE_UNKNOWN:
            break;
        }

    }
};

, WifiManager.WIFI_STATE_ENABLED, , , WiFi , . , , , .

+5

If you want to listen to the signal level, you can also listen to:

WifiManager.RSSI_CHANGED_ACTION

+1
source

All Articles