前言
最近因为工作的需要,要自定义展示Wifi信号强弱的需要,就通过利用系统广播的方式实现了一个自定义View——WifiStateView,下面话不多说了,感兴趣的朋友们一起来看看详细的介绍吧。
实现的效果图如下所示:
用不同的图片来表示Wifi信号的强弱,可以自定义Wifi信号等级
图标简陋了点,根据需要来替换即可
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); }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容猜你喜欢
AI推送时光机Android自定义View展示Wifi信号强弱指示方法示例
后端开发2023-05-30
IOS开发自定义view方法规范示例
后端开发2024-04-02
Android自定义View实现游戏摇杆键盘的方法示例
后端开发2023-05-30
Android利用Paint自定义View实现进度条控件方法示例
后端开发2023-05-30
Android自定义view之仿支付宝芝麻信用仪表盘示例
后端开发2022-06-06
Android编程实现AlertDialog自定义弹出对话框的方法示例
后端开发2023-05-30
Android 自定义标题栏 显示网页加载进度的方法实例
后端开发2022-06-06
Android编程实现自定义输入法功能示例【输入密码时防止第三方窃取】
后端开发2022-06-06
咦!没有更多了?去看看其它编程学习网 内容吧