如何使用 SUPPLICANT_STATE_CHANGED_ACTION WiFi BroadcastReceiver - android

发布于 2024-12-26 15:23:27 字数 106 浏览 2 评论 0原文

我想在我的设备连接到 wifi 网络时在屏幕上显示连接过程。 SUPPLICANT_STATE_CHANGED_ACTION 由 WifiManager 提供,但我不知道如何使用它。有人可以帮我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2025-01-02 15:23:27

您确实可以将广播意图用于 SUPPLICANT_STATE_CHANGED_ACTION

应用程序需要其清单文件中的权限:

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

然后注册系统广播:

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

registerReceiver() 需要一个实现 BroadcastReceiver 作为其第一个参数的类。在该代码中,您可以通过重写 onReceive 方法来对 Wifi 状态更改进行操作。例如,

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:
                    ...
            }
        }
    }
}

有关可能的 Wifi 状态值,请参阅 http://developer.android .com/reference/android/net/wifi/SupplicantState.html

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

痴者 2025-01-02 15:23:27

我不知道有什么回调方法可以让您知道 wifi 状态何时发生变化。我使用在后台运行的处理程序轮询信息。

将处理程序添加到您的类中。

private WifiStatusHandler wifiStatusHandler = new WifiStatusHandler();

通过调用启动它

wifiStatusHandler.start();

我使用的代码如下。

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文