您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
前言
最近因为工作的需要,要自定义展示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
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。