5

我正在手机中创建wifi 热点,我希望在打开或关闭热点时获取其状态更改事件。

4

4 回答 4

8

请看下面的代码。这将帮助你

public class WifiApManager {
    private final WifiManager mWifiManager;

    public WifiApManager(Context context) {
        mWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    }

    /*the following method is for getting the wifi hotspot state*/

    public WIFI_AP_STATE getWifiApState() {
        try {
            Method method = mWifiManager.getClass().getMethod("getWifiApState");

            int tmp = ((Integer) method.invoke(mWifiManager));

            // Fix for Android 4
            if (tmp > 10) {
                tmp = tmp - 10;
            }

            return WIFI_AP_STATE.class.getEnumConstants()[tmp];
        } catch (Exception e) {
            Log.e(this.getClass().toString(), "", e);
            return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
        }
    }

    /**
     * Return whether Wi-Fi Hotspot is enabled or disabled.
     * 
     * @return {@code true} if Wi-Fi AP is enabled
     * @see #getWifiApState()
     */
    public boolean isWifiApEnabled() {
        return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
    }

}

其中 WIFI_AP_STATE 是一个枚举,如下所示

  public enum WIFI_AP_STATE {
      WIFI_AP_STATE_DISABLING, 
      WIFI_AP_STATE_DISABLED, 
      WIFI_AP_STATE_ENABLING, 
      WIFI_AP_STATE_ENABLED, 
      WIFI_AP_STATE_FAILED
  }
于 2012-09-21T13:41:54.040 回答
7

要获取热点 AP 的当前状态,我使用:

final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final int apState = (Integer) wifiManager.getClass().getMethod("getWifiApState").invoke(wifiManager);

if (apState == 13) {
    // Ap Enabled
}

要在热点 AP 启用/禁用时获取更新,请在 BroadcastReceiver 中接收“android.net.wifi.WIFI_AP_STATE_CHANGED”意图:

public class WifiAPReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == "android.net.wifi.WIFI_AP_STATE_CHANGED") {
            int apState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            if (apState == 13) {
                // Hotspot AP is enabled
            } else {
                // Hotspot AP is disabled/not ready
            }
        }
    }
}

另外,不要忘记您在 Manifest 中的声明和权限:

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

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
于 2016-03-22T18:47:28.367 回答
0

您可以使用 BroadcastReciver 监控 wifi 状态。这是一个如何实现它的示例: http: //silverballsoftware.com/android-monitor-wifi-state-with-broadcastreceiver

于 2012-08-07T08:02:35.290 回答
-2

尝试这个

将此代码写入 OnCreate() 方法

ConnectivityManager cm = (ConnectivityManager) getSystemService(con.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {

            Success 
        }

        else {
                       Give Some Error Message
             }

还提到清单文件的必要权限

于 2012-08-07T11:24:49.347 回答