前言
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序定制開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了西寧免費建站歡迎大家使用!
最近因為工作的需要,要自定義展示W(wǎng)ifi信號強弱的需要,就通過利用系統(tǒng)廣播的方式實現(xiàn)了一個自定義View——WifiStateView,下面話不多說了,感興趣的朋友們一起來看看詳細的介紹吧。
實現(xiàn)的效果圖如下所示:
用不同的圖片來表示W(wǎng)ifi信號的強弱,可以自定義Wifi信號等級
圖標(biāo)簡陋了點,根據(jù)需要來替換即可
/** * 作者: 葉應(yīng)是葉 * 時間: 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 WeakReferencestateViewWeakReference; 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連接狀態(tài)變化 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
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位Android開發(fā)者們的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。