Android 自定义Wifi信号指示View

最近有需要自定义展示Wifi信号强弱的需要,就通过利用系统广播的方式实现了一个自定义View——WifiStateView

实现的效果图如下所示:

这里写图片描述

用不同的图片来表示Wifi信号的强弱,可以自定义Wifi信号等级 图标简陋了点,根据需要来替换即可

/**
 * 作者: 叶应是叶
 * 时间: 2017/8/22 18:25
 * 描述:
 */
public class WifiStateView extends AppCompatImageView {

    private WifiManager wifiManager;

    private WifiHandler wifiHandler;

    //没有开启Wifi或开启了Wifi但没有连接
    private static final int LEVEL_NONE = 0;

    //Wifi信号等级(最弱)
    private static final int LEVEL_1 = 1;

    //Wifi信号等级
    private static final int LEVEL_2 = 2;

    //Wifi信号等级
    private static final int LEVEL_3 = 3;

    //Wifi信号等级(最强)
    private static final int LEVEL_4 = 4;

    private final String TAG = "WifiStateView";

    private static class WifiHandler extends Handler {

        //虚引用
        private WeakReference<WifiStateView> stateViewWeakReference;

        WifiHandler(WifiStateView wifiStateView) {
            stateViewWeakReference = new WeakReference<>(wifiStateView);
        }

        @Override
        public void handleMessage(Message msg) {
            WifiStateView wifiStateView = stateViewWeakReference.get();
            if (wifiStateView == null) {
                return;
            }
            switch (msg.what) {
                case LEVEL_1:
                    wifiStateView.setImageResource(R.drawable.wifi_1);
                    break;
                case LEVEL_2:
                    wifiStateView.setImageResource(R.drawable.wifi_2);
                    break;
                case LEVEL_3:
                    wifiStateView.setImageResource(R.drawable.wifi_3);
                    break;
                case LEVEL_4:
                    wifiStateView.setImageResource(R.drawable.wifi_4);
                    break;
                case LEVEL_NONE:
                default:
                    wifiStateView.setImageResource(R.drawable.wifi_none);
                    break;
            }
        }
    }

    private BroadcastReceiver wifiStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e(TAG, "action " + intent.getAction());
            switch (intent.getAction()) {
                case WifiManager.WIFI_STATE_CHANGED_ACTION:
                    if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLING) {
                        wifiHandler.sendEmptyMessage(LEVEL_NONE);
                    }
                    break;
                case WifiManager.RSSI_CHANGED_ACTION:
                    if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLED) {
                        wifiHandler.sendEmptyMessage(LEVEL_NONE);
                        return;
                    }
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5);
                    Log.e(TAG, "level:" + level);
                    wifiHandler.sendEmptyMessage(level);
                    break;
            }
        }
    };


    public WifiStateView(Context context) {
        this(context, null);
    }

    public WifiStateView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WifiStateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiHandler = new WifiHandler(this);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        IntentFilter intentFilter = new IntentFilter();
        //Wifi连接状态变化
        intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        //Wifi信号强度变化
        intentFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
        getContext().registerReceiver(wifiStateReceiver, intentFilter);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        wifiHandler.removeCallbacksAndMessages(null);
        getContext().unregisterReceiver(wifiStateReceiver);
    }

}

这里提供代码下载:WifiStateView

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

6948
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

32010
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2665
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.2K7
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

2070
来自专栏魂祭心

原 canvas绘制clock

4164
来自专栏跟着阿笨一起玩NET

c#实现打印功能

2812
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2586
来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2597
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3195

扫码关注云+社区