欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android 监听WiFi的开关状态实现代码

时间:2022-02-04 08:54:45|栏目:Android代码|点击:

Android 监听WiFi的开关状态实现代码

WifiSwitch_Presenter 源码:

package com.yiba.wifi.sdk.lib.presenter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;

/**
 * Created by ${zhaoyanjun} on 2017/3/29.
 * Wifi 开关监听
 */

public class WifiSwitch_Presenter {
  private Context mContext ;
  private Receiver receiver ;
  private WifiSwitch_Interface mInterface ;


  public WifiSwitch_Presenter( Context context , WifiSwitch_Interface mInterface ){
    this.mContext = context ;
    this.mInterface = mInterface ;

    observeWifiSwitch();
  }

  private void observeWifiSwitch(){
    IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    receiver = new Receiver() ;
    mContext.registerReceiver(receiver, filter);
  }

  /**
   * 释放资源
   */
  public void onDestroy(){
    if ( receiver != null ){
      mContext.unregisterReceiver( receiver );
    }
    if (mContext!=null){
      mContext = null;
    }
  }

  class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
      int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
      switch (wifiState) {
        case WifiManager.WIFI_STATE_DISABLED:
          if (mInterface != null){
            mInterface.wifiSwitchState(WifiSwitch_Interface.WIFI_STATE_DISABLED);
          }
          break;
        case WifiManager.WIFI_STATE_DISABLING:
          if (mInterface != null){
            mInterface.wifiSwitchState(WifiSwitch_Interface.WIFI_STATE_DISABLING);
          }
          break;
        case WifiManager.WIFI_STATE_ENABLED:
          if (mInterface != null){
            mInterface.wifiSwitchState(WifiSwitch_Interface.WIFI_STATE_ENABLED);
          }
          break;
        case WifiManager.WIFI_STATE_ENABLING:
          if ( mInterface != null ) {
            mInterface.wifiSwitchState(WifiSwitch_Interface.WIFI_STATE_ENABLING);
          }
          break;
        case WifiManager.WIFI_STATE_UNKNOWN:
          if ( mInterface != null ){
            mInterface.wifiSwitchState( WifiSwitch_Interface.WIFI_STATE_UNKNOWN );
          }
          break;
      }
    }
  }
}

WifiSwitch_Interface 源码

package com.yiba.wifi.sdk.lib.presenter;

/**
 * Created by ${zhaoyanjun} on 2017/3/29.
 * Wifi 开关监听
 */

public interface WifiSwitch_Interface {

  int WIFI_STATE_ENABLING = 0 ;
  int WIFI_STATE_ENABLED = 1 ;
  int WIFI_STATE_DISABLING = 2 ;
  int WIFI_STATE_DISABLED = 3 ;
  int WIFI_STATE_UNKNOWN = 4 ;

  void wifiSwitchState( int state );
}

使用方式 MainActivity :

package com.yiba.core;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements WifiSwitch_Interface {

  private WifiSwitch_Presenter wifiSwitch_presenter ;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wifiSwitch_presenter = new WifiSwitch_Presenter( this , this ) ;

  }

  @Override
  public void wifiSwitchState(int state) {
    switch ( state ){
      case WifiSwitch_Interface.WIFI_STATE_DISABLED :
        Toast.makeText(this, "WiFi 已经关闭", Toast.LENGTH_SHORT).show();
        break;
      case WifiSwitch_Interface.WIFI_STATE_DISABLING:
        Toast.makeText(this, "WiFi 正在关闭", Toast.LENGTH_SHORT).show();
        break;
      case WifiSwitch_Interface.WIFI_STATE_ENABLED :
        Toast.makeText(this, "WiFi 已经打开", Toast.LENGTH_SHORT).show();
        break;
      case WifiSwitch_Interface.WIFI_STATE_ENABLING :
        Toast.makeText(this, "WiFi 正在打开", Toast.LENGTH_SHORT).show();
        break;
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();

    //释放资源
    if ( wifiSwitch_presenter != null ){
      wifiSwitch_presenter.onDestroy();
    }
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:android开发教程之系统资源的使用方法 android资源文件

栏    目:Android代码

下一篇:Android如何自定义EditText下划线?

本文标题:Android 监听WiFi的开关状态实现代码

本文地址:http://www.codeinn.net/misctech/192387.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有