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 监听 WiFi 开关状态的更多相关文章

  1. Android 监听wifi广播的两种方式

    1.XML中声明 <receiver android:name=".NetworkConnectChangedReceiver" >             <i ...

  2. Android监听WIFI网络的变化并且获得当前信号强度

    MainActivity如下: package cc.testwifi; import android.os.Bundle; import android.app.Activity; /** * De ...

  3. Android 监听电量的状态

    监控手机电量的变化和充电状态 在BatteryManager中有一个粘性广播,不需要BroadcastReceiver作为接收器,在注册时将传入null IntentFilter filter = n ...

  4. [Android Pro] 监听WIFI 打开广播

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-perm ...

  5. 广播小案例-监听系统网络状态 --Android开发

    本例通过广播实现简单的监听系统网络状态改变的小案例. 1.案例效果演示 当手机连网后,系统提示“已连接网络”, 当手机断网后,系统提示“当前网络不可用”. 2.案例实现 在主活动中动态注册广播,然后写 ...

  6. Android监听手机网络变化

    Android监听手机网络变化 手机网络状态发生变化会发送广播,利用广播接收者,监听手机网络变化 效果图 注册广播接收者 <?xml version="1.0" encodi ...

  7. android86 监听SD卡状态,勒索软件,监听应用的安装、卸载、更新,无序广播有序广播

    * 添加权限 <uses-permission android:name="android.permission.RECEIVE_SMS"/> * 4.0以后广播接收者 ...

  8. 【Android】Android 监听apk安装替换卸载广播

    [Android]Android 监听apk安装替换卸载广播 首先是要获取应用的安装状态,通过广播的形式 以下是和应用程序相关的Broadcast Action ACTION_PACKAGE_ADDE ...

  9. Windows Phone 8 获取与监听网络连接状态(转)

    原文地址:http://www.cnblogs.com/sonic1abc/archive/2013/04/02/2995196.html 现在的只能手机对网络的依赖程度都很高,尤其是新闻.微博.音乐 ...

随机推荐

  1. (转)centos 7 Tomcat 8.5 的安装及生产环境的搭建调优

    原文:https://www.cnblogs.com/linhankbl/articles/9149804.html#top JVM菜鸟进阶高手之路七(tomcat调优以及tomcat7.8性能对比) ...

  2. c++中double类型控制小数位数

    有时,我们需要输出确定小数位数的double,可以先引入如下头文件: #include <iomanip> 然后通过下列方式输出: double zzz = 8.66666; cout & ...

  3. linux文件权限说明

    # ll total 0 drwxr-xr-x. 2 root root 6 Aug 28 11:07 test1 drwxr-xr-x. 2 root root 6 Aug 28 11:07 tes ...

  4. 【杂谈】Java I/O的底层实现

    前言 Java I/O功能封装的很好,使用起来很方便,就是刚开始学的时候,如果不了解装饰器模式,会被他繁多的类给吓到.用多了也就习惯了,而且现在有很多实用的封装良好的实用类,可直接读写整个文件.开发者 ...

  5. <Think Python>中统计文献单词的处理代码

    def process_line(line, hist):    """Adds the words in the line to the histogram. Modi ...

  6. AD分类论文研读(1)

    转移性学习对阿尔茨海默病分类的研究 原文链接 摘要 将cv用于研究需要大量的训练图片,同时需要对深层网络的体系结构进行仔细优化.该研究尝试用转移学习来解决这些问题,使用从大基准数据集组成的自然图像得到 ...

  7. Postsharp 破解工具(通杀版,持续更新)

    2019.04.18 重要说明 VS2019 正式版已经发布了,Postsharp v6.2.2-Preview(预览版)也开始支持VS2019.不过截至目前,该预览版还不是特别稳定,因此提醒下大家在 ...

  8. Mac Hadoop的安装与配置

    这里介绍Hadoop在mac下的安装与配置. 安装及配置Hadoop 首先安装Hadoop $ brew install Hadoop 配置ssh免密码登录 用dsa密钥认证来生成一对公钥和私钥: $ ...

  9. 在ASP.NET MVC使用JavaScriptResult

    本例中,我们尝试把javascript程序搬至控制器中去.更好地在控制器编写程序. 首先来看看原来的写法. 在SepController控制器,添加如下操作: public ActionResult ...

  10. AngularJS学习笔记(二)MVVM

    什么是MVVM MVVM模式是Model-View-ViewMode模式的简称. 由视图(View).视图模型(ViewModel).模型(Model)三部分组成,通过这三部分实现UI逻辑.呈现逻辑和 ...