日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 监听 WiFi 开关状态

發布時間:2024/9/30 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 监听 WiFi 开关状态 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android 監聽 WiFi 開關狀態

轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/70854309
本文出自【趙彥軍的博客】

  • 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 {@Overridepublic 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 ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);wifiSwitch_presenter = new WifiSwitch_Presenter( this , this ) ;}@Overridepublic 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;}}@Overrideprotected void onDestroy() {super.onDestroy();//釋放資源if ( wifiSwitch_presenter != null ){wifiSwitch_presenter.onDestroy();}} }

總結

以上是生活随笔為你收集整理的Android 监听 WiFi 开关状态的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。