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();
    }
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(0)

相关推荐

  • Android中ToggleButton开关状态按钮控件使用方法详解

    ToggleButton开关状态按钮控件使用方法,具体内容如下 一.简介 1. 2.ToggleButton类结构 父类是CompoundButton,引包的时候注意下 二.ToggleButton开关状态按钮控件使用方法 1.新建ToggleButton控件及对象 private ToggleButton toggleButton1; toggleButton1=(ToggleButton) findViewById(R.id.toggleButton1); 2.设置setOnCheckedC

  • android基本控件ToggleButton&Switch使用指南

    ToggleButton(开关按钮)和Switch(开关)讲解: 一.核心属性讲解: (1)ToggleButton textOn:按钮被选中的时候文字显示 textOff:按钮没有被选中的时候文字显示 ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的显示文本. 以下案例为ToggleButton的用法 目录结构 main.xml布局文件 <?xml version="1.0" encoding="utf-8"?> <

  • Android开发之ToggleButton实现开关效果示例

    本文实例讲述了Android使用ToggleButton实现开关效果的方法.分享给大家供大家参考,具体如下: activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="matc

  • Android 判断屏幕开关状态方式总结

    Android判断屏幕开关状态  方法一:使用系统服务 PowerManager pm= (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); if(!pm.isScreenOn()) { //熄屏状态 } 方法二:使用广播 public class ScreenReceiver extends BroadcastReceiver { @Override public void onReceive(Context con

  • Android控件ToggleButton多状态按钮使用详解

    什么是ToggleButton? ToggleButton一般有两种状态:选中和未选中 并且需要为不同状态设置不同的文本 ToggleButton属性 android:checked="true"--当前按钮状态,选中为"true",未选中为"false" android:textOn="开" android:checked="true"的时候,显示 取决于checked的状态,即当checked=&quo

  • Android ToggleButton 详解及实例代码

    Android ToggleButton 详解 在Android的开发过程中,对于ToggleButton的使用频率也是相当的高的,下面我就来说一下,这个组件的两种使用方式. 第一种是简单的使用,利用Toast的方式弹出提示语句 需要注意的是要想自定义ToggleButton的显示的内容,就需要设置其TextOn和TextOff的内容. <ToggleButton android:id="@+id/toggleButton1" android:layout_width="

  • Android控件之ToggleButton的使用方法

    ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的显示文本. 以下案例为ToggleButton的用法 目录结构 main.xml布局文件 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    androi

  • 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

  • Android 监听手机GPS打开状态实现代码

    Android 监听手机GPS打开状态实现代码 GPS_Presenter package com.yiba.core; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.location.LocationManager; /** *

  • Android监听手机短信的示例代码

    本文介绍了Android监听手机短信的示例代码,分享给大家,具体如下: 以下情况可能会导致短信拦截失败: 小米,360等品牌手机拦截短信,短信的优先级给了系统 用户禁用短信权限 手机连接电脑,被电脑端的手机助手类软件截获 手机内装有QQ通讯录之类的管理联系人,短信的应用,被截获. 前提--权限: <uses-permission android:name="android.permission.RECEIVE_SMS" > </uses-permission>

  • Android中监听软键盘显示状态实现代码

    /**监听软键盘状态 * @param activity * @param listener */ public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) { final View decorView = activity.getWindow().getDecorView(); decorView.getViewTree

  • Android 监听应用前/后台切换实例代码

    前言 这周接到一个需求,需要在应用从后台切换到前台时,展示我们的广告.展示页面其实可以复用以前的开屏广告页,唯一的问题就是如何监听应用从后台切到了前台. 正文 在众多方法中,我采用了以下这种方式.废话不多说,咱们直接看代码: //自定义Application类 public class MyApplication extends Application 在清单文件中声明 <application android:name=".MyApplication" android:allo

  • android 监听网络状态的变化及实战的示例代码

    平时我们在请求错误的情况下,通常会进行处理一下,一般来说,主要分为两方面的错误 没有网络的错误 在有网络的情况下,我们客户端的错误或者服务器端的错误 今天这篇博客主要阐述以下问题 怎样监听网络状态的变化,包括是否打开WiFi,否打开数据网络,当前连接的网络是否可用 网络没有打开情况下的处理,如弹出对话框,跳转到 打开 WiFi设置的界面等 非WiFi情况下是否加载图片,是否播放视频等 实现思路 在网络错误的情况下获取网络状态进行判断,这种方法是可行的,但你想过了没有,如果每次都要进行这样的判断,

  • flutter监听app进入前后台状态的实现

    目录 1. 监听app进入前后台状态 示例代码: 2.其它状态监听 在开发app的过程中,我们经常需要根据app的前后台的状态,做一些事情,那么我们在flutter中是如何实现这一监听的? flutter给我们提供了WidgetsBindingObserver来进行一些状态的判断,但是判断前后台的状态只是该API种其中一种功能,对于WidgetsBindingObserver需要注意两点 最好是先进入而且不会销毁的页面, 这可以判断整个程序的前后台状态(例如:main.dart类中) Widge

  • Android 监听网络状态方法详解

    Android 监听网络状态方法详解 一.加入网络权限 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission Android:name="android.permission.ACCESS_NETWORK_STATE" /> 二.判断手机网络的几个方案 1)判断是否有网络连接 public boolean isMobileConnected(Context context) { if (context != nul

  • Android 监听软键盘状态的实例详解

    Android 监听软键盘状态的实例详解 近日遇到要检测软键盘是否显示或隐藏的问题,搜了一下网上,最后找到一个很简单的,记录一下. activityRoot是activity的根view,就是xml里面的第一个view,给它设置一个id. final View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(ne

  • Android监听电池状态实例代码

    如果要监听电池的状态改变,需要动态注册:android.intent.action.BATTERY_CHANGED,收到Action后可以根据对应的Key获取你需要的信息,更详细信息可以参考以下例子中的BatteryChangedReceiver类 具体代码如下所示: package com.example.charginganimation; import android.app.Activity; import android.content.BroadcastReceiver; impor

随机推荐