欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-05 22:30:46
android 监听wifi的开关状态实现代码 wifiswitch_presenter 源码: package com.yiba.wifi.sdk.lib...

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();
    }
  }
}

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