Android自定义View展示Wifi信号强弱指示方法示例-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

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

前言

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了西宁免费建站欢迎大家使用!

最近因为工作的需要,要自定义展示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 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开发者们的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对创新互联的支持。


新闻名称:Android自定义View展示Wifi信号强弱指示方法示例
标题链接:http://kswsj.cn/article/geojdg.html

其他资讯