当前位置:主页 > 移动开发 > Android代码 >

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

时间:2020-10-16 13:10:00 | 栏目:Android代码 | 点击:

前言

最近因为工作的需要,要自定义展示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);
  }

}

这里提供代码下载:

github地址:https://github.com/leavesC/WifiStateView

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

总结

您可能感兴趣的文章:

相关文章