Android打开WLAN开关的广播状态监听

Android打开WLAN开关的广播状态监听

Android中WLAN的广播监听的是WLAN状态的改变 ,Action是WifiManager.WIFI_STATE_CHANGED_ACTION,WLAN的状态系统定义了5种,分别是

WIFI_STATE_DISABLED    WLAN已经关闭
WIFI_STATE_DISABLING   WLAN正在关闭
WIFI_STATE_ENABLED     WLAN已经打开
WIFI_STATE_ENABLING    WLAN正在打开
WIFI_STATE_UNKNOWN     未知

下面看实例代码:

public class WLANListener {

    private Context mContext;
    private WLANBroadcastReceiver receiver;
    private WLANStateListener mWLANStateListener;

    public WLANListener(Context context) {
        mContext = context;
        receiver = new WLANBroadcastReceiver();
    }

    public void register(WLANStateListener listener) {
        mWLANStateListener = listener;
        if (receiver != null) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
            mContext.registerReceiver(receiver, filter);
        }
    }

    public void unregister() {
        if (receiver != null) {
            mContext.unregisterReceiver(receiver);
        }
    }

    private class WLANBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                String action = intent.getAction();
                /** wifi状态改变 */
                if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
                    if (mWLANStateListener != null) {
                        Log.e("zhang", "WLANBroadcastReceiver --> onReceive--> WIFI_STATE_CHANGED_ACTION");
                        mWLANStateListener.onStateChanged();
                    }
                }
                /**
                 * WIFI_STATE_DISABLED    WLAN已经关闭
                 * WIFI_STATE_DISABLING   WLAN正在关闭
                 * WIFI_STATE_ENABLED     WLAN已经打开
                 * WIFI_STATE_ENABLING    WLAN正在打开
                 * WIFI_STATE_UNKNOWN     未知
                 */
                int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
                switch (state) {
                    case WifiManager.WIFI_STATE_DISABLED:
                        if (mWLANStateListener != null) {
                            Log.e("zhang", "WLANBroadcastReceiver --> onReceive--> WIFI_STATE_DISABLED");
                            mWLANStateListener.onStateDisabled();
                        }
                        break;
                    case WifiManager.WIFI_STATE_DISABLING:
                        if (mWLANStateListener != null) {
                            Log.e("zhang", "WLANBroadcastReceiver --> onReceive--> WIFI_STATE_DISABLING");
                            mWLANStateListener.onStateDisabling();
                        }
                        break;
                    case WifiManager.WIFI_STATE_ENABLED:
                        if (mWLANStateListener != null) {
                            Log.e("zhang", "WLANBroadcastReceiver --> onReceive--> WIFI_STATE_ENABLED");
                            mWLANStateListener.onStateEnabled();
                        }
                        break;
                    case WifiManager.WIFI_STATE_ENABLING:
                        if (mWLANStateListener != null) {
                            Log.e("zhang", "WLANBroadcastReceiver --> onReceive--> WIFI_STATE_ENABLING");
                            mWLANStateListener.onStateEnabling();
                        }
                        break;
                    case WifiManager.WIFI_STATE_UNKNOWN:
                        if (mWLANStateListener != null) {
                            Log.e("zhang", "WLANBroadcastReceiver --> onReceive--> WIFI_STATE_UNKNOWN");
                            mWLANStateListener.onStateUnknow();
                        }
                        break;
                }
            }
        }
    }


    public interface WLANStateListener {
        void onStateChanged();

        void onStateDisabled();

        void onStateDisabling();

        void onStateEnabled();

        void onStateEnabling();

        void onStateUnknow();

    }
}

在MianActivity中调用:

public class MainActivity extends AppCompatActivity {

    private WLANListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listener = new WLANListener(this);
        listener.register(new WLANListener.WLANStateListener() {
            @Override
            public void onStateChanged() {
                Log.e("zhang", "MainActivity --> onStateChanged--> ");
            }

            @Override
            public void onStateDisabled() {
                Log.e("zhang", "MainActivity --> onStateDisabled--> ");
            }

            @Override
            public void onStateDisabling() {
                Log.e("zhang", "MainActivity --> onStateDisabling--> ");
            }

            @Override
            public void onStateEnabled() {
                Log.e("zhang", "MainActivity --> onStateEnabled--> ");
            }

            @Override
            public void onStateEnabling() {
                Log.e("zhang", "MainActivity --> onStateEnabling--> ");
            }

            @Override
            public void onStateUnknow() {
                Log.e("zhang", "MainActivity --> onStateUnknow--> ");
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (listener != null) {
            listener.unregister();
        }
        super.onDestroy();
    }
}

你可能感兴趣的:(android提升)