How to use SUPPLICANT_STATE_CHANGED_ACTION WiFi BroadcastReceiver - android

房东的猫 提交于 2019-12-14 03:49:43

问题


I want to show the connection process on the screen when my device is connecting to the wifi network. SUPPLICANT_STATE_CHANGED_ACTION is provided by WifiManager but i don't know how to use it. Can anyone help me please?


回答1:


I don't know of a callback method that lets you know when the wifi status has changed. I polled the information using a Handler running in the background.

Add the handler to your class.

private WifiStatusHandler wifiStatusHandler = new WifiStatusHandler();

Start it by calling

wifiStatusHandler.start();

The code I used is below.

/**
 * Checks for wifi status updates.
 */
private class WifiStatusHandler extends Handler {
    private boolean running = false;

    public void handleMessage(Message message) {
        if (running) {
            //check wifi status here
            WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
            int curWifiState = wifiMgr.getWifiState();
            SupplicantState info = wifiMgr.getConnectionInfo().getSupplicantState();
            WifiInfo curWifi = wifiMgr.getConnectionInfo();
            Log.i(TAG,"WIFI STATE = " + info.toString());
            //update the TextView etc.
            sleep();
        }
    }

    private void sleep() {
        removeMessages(0);
        sendMessageDelayed(obtainMessage(0), REFRESH_DELAY);
    }

    public synchronized void start() {
        running = true;
        removeMessages(0);
        sendMessageDelayed(obtainMessage(0), 0);
    }

    public synchronized void stop() {
        running = false;
    }
}



回答2:


You can indeed use the broadcasted intents for SUPPLICANT_STATE_CHANGED_ACTION:

The app needs the permission in its Manifest file:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Then register for the system broadcast:

    MyWifiStateReceiver handler = new MyWifiStateReceiver();
    context.registerReceiver(handler, new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));

the registerReceiver() needs an instance of a class implementing BroadcastReceiver as its first argument. In that code you can act on the Wifi state changes by overriding the onReceive method. For example

public class MyWifiStateReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION))
        {
            SupplicantState state = (SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
            switch(state)
            {
                case COMPLETED:
                case DISCONNECTED:
                    ...
            }
        }
    }
}

For the possible Wifi state values, see http://developer.android.com/reference/android/net/wifi/SupplicantState.html



来源:https://stackoverflow.com/questions/8847818/how-to-use-supplicant-state-changed-action-wifi-broadcastreceiver-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!