温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android 应用中怎么监听WiFi的开关状态

发布时间:2020-12-04 17:33:28 来源:亿速云 阅读:650 作者:Leah 栏目:移动开发

Android 应用中怎么监听WiFi的开关状态?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI