91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android 應用中怎么監聽WiFi的開關狀態

發布時間:2020-12-04 17:33:28 來源:億速云 閱讀:651 作者: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

密山市| 密云县| 金门县| 临桂县| 屏南县| 深州市| 宁国市| 阜平县| 崇阳县| 阳原县| 保康县| 武川县| 革吉县| 贺兰县| 那曲县| 松滋市| 亳州市| 富蕴县| 陇南市| 赣榆县| 延长县| 城步| 凤台县| 陆良县| 密山市| 通许县| 华坪县| 格尔木市| 双江| 日喀则市| 米林县| 汉中市| 勐海县| 贺兰县| 琼结县| 清涧县| 民权县| 大庆市| 邯郸市| 筠连县| 东海县|