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

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

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 {        @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();        }    }}
你可能感兴趣的文章
android expandablelistview--实现类似qq界面的效果
查看>>
CentOS 6.8 安装Nginx
查看>>
spring发布和接收定制的事件(spring事件传播)
查看>>
javascript内置对象
查看>>
Eclipse注释模板与规范
查看>>
open***客户端获得固定IP
查看>>
离线安装paramiko
查看>>
IDA文件目录简介
查看>>
求模版函数地址
查看>>
使用 Apache POI 处理 Microsoft Office 文档
查看>>
10个实用的但偏执的Java编程技术
查看>>
bash批量创建目录
查看>>
Windows pscp
查看>>
初学Python的学习笔记8----面向对象、数据封装、访问限制、继承和多态
查看>>
CAS注销后自定义跳转路径
查看>>
[大数据量]布隆过滤器(Bloom Filter)适用类型以及具体示例
查看>>
Linux | OOM机制的理解
查看>>
linux启动nagios无法通过web访问解决
查看>>
OpenSessionInViewFilter原理以及为什么要用OpenSessionInViewFilter
查看>>
决策树
查看>>