Android 감청 와 이 파이 의 스위치 상태 구현 코드

Android 감청 와 이 파이 의 스위치 상태 구현 코드
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_인터페이스 소스 코드

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

읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기