监测热点状态的Andr​​oid热点、状态、oid、Andr

2023-09-05 05:12:18 作者:离人未归丶

我是新来的机器人。 我想通过的BroadcastReceiver 来接收信息(的onReceive )知道,如果用户启用/禁用 便携式Wi-Fi热点 (设置 - >无线和网络 - >绑定与放大器;便携式热点) 。 勾选此链接 而且我发现,有 android.net.wifi.WIFI_AP_STATE_CHANGED ,但它被设置为隐藏。任何我可以如何使用???

I'm new to android. I want to receive information via broadcastreceiver (onReceive) to know that if user enable/disable "Portable Wi-Fi Hotspot" (Settings->Wireless &Networks->Tethering & portable hotspot). Check this link And I found that there is "android.net.wifi.WIFI_AP_STATE_CHANGED" but it was set to hidden. Any how I can use that ???

在此先感谢

推荐答案

接受启用/禁用便携式Wi-Fi热点事件,您将需要为WIFI_AP_STATE_CHANGED作为注册一个接收器:

to receive enable/disable "Portable Wi-Fi Hotspot" events you will need to register an Receiver for WIFI_AP_STATE_CHANGED as :

mIntentFilter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED");
registerReceiver(mReceiver, mIntentFilter);

里面的BroadcastReceiver的onReceive我们可以使用提取的WiFi热点状态 wifi_state 为:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if ("android.net.wifi.WIFI_AP_STATE_CHANGED".equals(action)) {

             // get Wi-Fi Hotspot state here 
            int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);

            if (WifiManager.WIFI_STATE_ENABLED == state % 10) {
                // Wifi is enabled
            }

        }
    }
};

您可以通过在AndroidManifest宣布接收机 android.net.wifi.WIFI_AP_STATE_CHANGED 动作做同样的,也包括 AndroidManifest所有必要的WiFi权限。 XML

you can do same by declaring Receiver in AndroidManifest for android.net.wifi.WIFI_AP_STATE_CHANGED action and also include all necessary wifi permissions in AndroidManifest.xml

编辑:

在AndroidManifest添加接收器为:

Add receiver in AndroidManifest as :

<receiver android:name=".WifiApmReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />
    </intent-filter>
</receiver>

您可以看到这个的例子更多的帮助

you can see this example for more help