博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 监听 WiFi 开关状态
阅读量:5737 次
发布时间:2019-06-18

本文共 4401 字,大约阅读时间需要 14 分钟。

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(); } }}
你可能感兴趣的文章
设计模式单列
查看>>
人像模式的灯光效果?iPhone 8开挂袭来
查看>>
Linux下MongoDB安装与配置
查看>>
DSL配置(PPPOA)
查看>>
WEBRTC执行流程
查看>>
Spring Boot 入门系列
查看>>
Spring Cloud版——电影售票系统<六>使用 Spring Cloud Config 统一管理微服务配置
查看>>
Java not support java EE1.3
查看>>
iptables规则备份及恢复、firewalld九个zone,service的操作
查看>>
www.conf配置文件的参数详解
查看>>
如何实现邀请好友帮抢票功能?
查看>>
深圳联通特邀湖北籍企业参观公司总部大楼举行
查看>>
告警系统主脚本、告警系统配置文件、告警系统监控项目
查看>>
Python 和 PyCharm 在 windows10 环境的安装和设置
查看>>
C语言入门基础之数组——数学和编程的完美结合(图)
查看>>
《远见》的读后感作文1000字范文
查看>>
重置密码、单用户模式、救援模式
查看>>
LAMP环境搭建1-mysql5.5
查看>>
第三课 Linux目录及文件管理、用户组管理及bash重定向
查看>>
shell 脚本攻略--小试牛刀
查看>>