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 {

        @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();
        }
    }
}
时间: 2024-10-24 06:33:46

Android 监听 WiFi 开关状态的相关文章

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监听手机电话状态与发送邮件通知来电号码的方法(基于PhoneStateListene实现)_Android

本文实例讲述了Android监听手机电话状态与发送邮件通知来电号码的方法.分享给大家供大家参考,具体如下: 在android中可以用PhoneStateListener来聆听手机电话状态(比如待机.通话中.响铃等).本例是通过它来监听手机电话状态,当手机来电时,通过邮件将来电号码发送到用户邮箱的例子.具体程序如下: import android.app.Activity; import android.content.Intent; import android.os.Bundle; impor

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

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

Android 监听wifi广播的两种方式

 1.XML中声明 <receiver android:name=".NetworkConnectChangedReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.wifi.WIFI_STATE_CHANGED"

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

MainActivity如下: package cc.testwifi; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 监听Wifi网络的变化并且获得当前信号强度 * * 参考资料: * http://blog.csdn.net/way_ping_li/article/details/8777478 * Thank you very much * */ public class MainActivit

ndroid 监听wifi总结

功能实现:控制wifi开关,连上某个特定的wifi. 首先先上个wifi工具类,此类转载网上一人,出处不明了.  [java] view plaincopy package rodar.rgs.conference.utils;      import java.lang.reflect.Constructor;   import java.lang.reflect.Field;   import java.lang.reflect.InvocationTargetException;   im

Android实现监听电话呼叫状态的方法_Android

本文实例讲述了Android实现监听电话呼叫状态的方法.分享给大家供大家参考.具体如下: 在清单文件AndroidManifest.xml中添加权限: 复制代码 代码如下: <uses-permission android:name="android.permission.READ_PHONE_STATE"/> java代码如下: // 取得电话服务 TelephonyManager telManager = (TelephonyManager) getSystemServ

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

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

Android 监听网络状态方法详解

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