欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义View展示Wifi信号强弱指示方法示例

程序员文章站 2023-12-14 17:09:16
前言 最近因为工作的需要,要自定义展示wifi信号强弱的需要,就通过利用系统广播的方式实现了一个自定义view——wifistateview,下面话不多说了,感兴趣的朋友...

前言

最近因为工作的需要,要自定义展示wifi信号强弱的需要,就通过利用系统广播的方式实现了一个自定义view——wifistateview,下面话不多说了,感兴趣的朋友们一起来看看详细的介绍吧。

实现的效果图如下所示:

Android自定义View展示Wifi信号强弱指示方法示例

用不同的图片来表示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);
  }

}

这里提供代码下载:

github地址:https://github.com/leavesc/wifistateview

本地下载:http://xiazai.jb51.net/201708/yuanma/wifistateview(jb51.net).rar

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位android开发者们的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

上一篇:

下一篇: