code
package com.idogfooding.lawsonsot.common; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import com.idogfooding.lawsonsot.model.WifiResultInfo; /** * * */ public class WiFiUtil { /** * 判断是否联网 */ public static boolean checkConnectNetwork(Context context) { ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo net = conn.getActiveNetworkInfo(); if (net != null && net.isConnected()) { return true; } return false; } /** * 获取已连接的wifi */ public static WifiResultInfo getConnectedWifi(Context context) { // Context context = SysApplication.getContext(); WifiResultInfo wifiResultInfo = null; WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo != null) { wifiResultInfo = new WifiResultInfo(); //名称 String ssid = wifiInfo.getSSID(); //信号强度,得到的值是一个0到-100的区间值,是一个int型数据 int rssi = wifiInfo.getRssi(); if(ssid.startsWith("\"") && ssid.endsWith("\"")) { ssid = ssid.substring(1, ssid.length() - 1); } //信号强度等级,根据强度值,分为6个等级,5最大,表示强度最高,0表示没连Wifi。 int level = WifiManager.calculateSignalLevel(rssi, 6); String supplicantState = wifiInfo.getSupplicantState().toString(); wifiResultInfo.setWifiLevel(level); wifiResultInfo.setRssi(rssi); wifiResultInfo.setSsid(ssid); wifiResultInfo.setSupplicantState(supplicantState); } return wifiResultInfo; } }
AndroidManifest.xml
<!-- 联网,网络状态 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- WIFI改变,WIFI状态 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 申请定位权限 否则的话,wifiManager.getScanResults()获取不到任何的数据 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
注册网络变化接收通知
/** * 注册网络变化接收通知 * */ public static void regNetChageRecever(Context context){ NetChangeReceiver receiver = new NetChangeReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); context.registerReceiver(receiver, intentFilter); }
Recever
package com.jay.common; import android.annotation.SuppressLint; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; /** * 接收 网络状态变化 发出的通知 * */ public class NetChangeReceiver extends BroadcastReceiver { @SuppressLint("UnsafeProtectedBroadcastReceiver") @Override public void onReceive(Context context, Intent intent) { try { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { //有网 String res = ""; Activity activity = BaseForm.curActivity(); activity.runOnUiThread(()->{ // activity.findViewById(R.id.txtDes); }); } else { //无网 } } catch (Exception e) { //ignore } } }
来源:https://blog.csdn.net/chongchi_wxcc/article/details/126941037