Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

android - Listening WIFI state

I want to set listener to listen on wireless state,can anyone 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 event that i use that i want to listen for wifi change, and the above code is all 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 anyone please help me creating a listener that will listen to the wifi state and check if wifi is connected or connecting if is not i want to enable data packet traffic through 3g/4g

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I came across that problem too, here's how I solved it.

In my activity onCreate(..) I did

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

and additionally i created the member 'mWifiStateChangedReceiver' this way

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;
        }

    }
};

As you can see, when I get WifiManager.WIFI_STATE_ENABLED I additionally test if the network is really connected, because a enabled WiFi doesn't mean it's connected. At least this was my guess, that's why I'm waiting till the network is really connected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...