最近有需要自定义展示Wifi信号强弱的需要,就通过利用系统广播的方式实现了一个自定义View——WifiStateView
实现的效果图如下所示:
这里写图片描述
用不同的图片来表示Wifi信号的强弱,可以自定义Wifi信号等级
图标简陋了点,根据需要来替换即可
/**
* 作者:leavesC
* 时间:2019/2/11 20:21
* 描述:
* GitHub:https://github.com/leavesC
* Blog:https://www.jianshu.com/u/9df45b87cfdf
*/
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 static 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) {
if (intent.getAction() != null) {
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);
}
}
网友评论