信号强弱检测
/**
* @ClassName: WifiCheckUtils
* @Description: 页面切换 检测wifi信号强弱
* @Author: 有时有晌
* @Date: 2022/1/26 16:00
*/
object WifiCheckUtils {
fun checkWifiLevle(context:Context,isConnect:Boolean):Int{
var wifiType = 3
try {
var wifiManager = context!!.getSystemService(Context.WIFI_SERVICE) as WifiManager
var wifiInfo = wifiManager.connectionInfo as WifiInfo
var wifiLever = wifiInfo.rssi
if (isConnect){
when (wifiLever) {
in -50..0 -> {//信号最强
wifiType = 0
}
in -70..-50 -> {//较强
wifiType = 1
}
in -80..-70 -> {//较弱
wifiType = 2
}
in -100..-80 -> {//微弱
wifiType = 3
}
else->{
wifiType = 3
}
}
LogUtil.e("TAG","==-->WiFi信号强度$wifiType")
setWifiLevel(wifiType)
}
} catch (e: Exception) {
}
return wifiType
}
/**
* 设置wifi信号等级,这里是设置MainActivity页面的wifiLevel,用来设置wifi强度
*/
private fun setWifiLevel(wifiLevel:Int){
try {
MainActivity.act.wifiLevel = wifiLevel
} catch (e: Exception) {
}
}
}
如上同时可以搭配检测wifi是否通畅代码一块使用
搭配网络实时流量检测使用
Android实时检测网络(流量监控)直接复制可用
检测网络是否正常
利用ping功能来实现,跟电脑上ping效果一样
具体实现
/**
* @ClassName: CheckNetStatus
* @Description: 检测网络状态是否通畅
* @Author: 有时有晌
* @Date: 2022/1/24 16:47
*/
object CheckNetStatus {
val TAG="CheckNetStatus"
fun check(mContext:Activity,netStatusCallBack: NetStatusCallBack){
var url ="www.baidu.com"
//TaskManager 这个类是我本地在用的,大家用时候随便开个线程什么的就可以了。
TaskManager.getInstance().execRunnable {
LogUtil.e(TAG,"==-->WIFI可用检测")
val stringBuilder = StringBuilder()
val netState = toCheck(url, stringBuilder)
LogUtil.e(TAG, "ping ${url} content \n${stringBuilder.toString()}")
LogUtil.e(TAG, "ping ${url} state:${netState}")
mContext.runOnUiThread {
netStatusCallBack.netStatus(netState)
}
}
}
fun toCheck(ipAddress: String, stringBuilder: StringBuilder): Boolean {
var result: String? = null
try {
var ip = "www.baidu.com" // ping 的地址,可以换成任何一种可靠的外网
if (!TextUtils.isEmpty(ipAddress)) {
ip = ipAddress
}
val p = Runtime.getRuntime().exec("ping -c 4 -w 100 $ip") // ping网址4次 响应100ms
// 读取ping的内容,可以不加
val input = p.inputStream
val bufferedReader = BufferedReader(InputStreamReader(input))
var content: String? = ""
while (bufferedReader.readLine().also { content = it } != null) {
stringBuilder.append(content)
stringBuilder.append("\n")
}
// ping的状态
val status = p.waitFor()
if (status == 0) {
result = "success"
return true
} else {
result = "failed" //PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data. //64 bytes from 180.101.49.11: icmp_seq=2 ttl=49 time=53.3 ms //64 bytes from 180.101.49.11: icmp_seq=3 ttl=49 time=51.3 ms //64 bytes from 180.101.49.11: icmp_seq=4 ttl=49 time=50.2 ms
}
} catch (e: IOException) {
result = "IOException"
} catch (e: InterruptedException) {
result = "InterruptedException"
} finally {
LogUtil.e(TAG, "result = $result")
}
return false
}
interface NetStatusCallBack{
fun netStatus(isConnect:Boolean)
}
}
调用
- 在需要响应的页面中增加对应的执行方法,依次调用即可,网络检测,信号检测,可根据需求更改为定时轮询请求,以保证实时检测效果
MainActivity中方法
/**
* wifi信号level
* @param wifiLevel
*/
public void setWifiLevel(int wifiLevel){
switch (wifiLevel) {
case 0:
iv_net_status.setBackgroundResource(R.drawable.wifi_success0);
break;
case 1:
iv_net_status.setBackgroundResource(R.drawable.wifi_success1);
break;
case 2:
iv_net_status.setBackgroundResource(R.drawable.wifi_success2);
break;
default:
iv_net_status.setBackgroundResource(R.drawable.wifi_success3);
break;
}
}
/**
* 设置网络连接状态
* 网络变化触发广播,广播变化触发检测网络
* @param isConnect
*/
public void setNetIv(boolean isConnect){
if (isConnect){
iv_net_status.setBackgroundResource(R.drawable.wifi_success3);
}else{
iv_net_status.setBackgroundResource(R.drawable.wifi_error);
}
CheckNetStatus.INSTANCE.check(this, new CheckNetStatus.NetStatusCallBack() {
@Override
public void netStatus(boolean isConnect) {
if (isConnect){
iv_net_status.setBackgroundResource(R.drawable.wifi_success);
setWifiLevel(WifiCheckUtils.INSTANCE.checkWifiLevle(MainActivity.this, true));
}else{
iv_net_status.setBackgroundResource(R.drawable.wifi_error);
}
}
});
}
贡献一波信号强度图片
![网络断开,看不到直接复制这个地址打开](https://s2.51cto.com/images/blog/202403/11115518_65ee80a6bd90460285.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![信号强度满格,看不到直接复制这个地址打开](https://s2.51cto.com/images/blog/202403/11115518_65ee80a6d384623531.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![信号3格,看不到直接复制这个地址打开](https://s2.51cto.com/images/blog/202403/11115519_65ee80a708e795803.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![信号2格,看不到直接复制这个地址打开](https://s2.51cto.com/images/blog/202403/11115519_65ee80a725b315861.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![信号一格,看不到直接复制这个地址打开](https://s2.51cto.com/images/blog/202403/11115519_65ee80a7433b880143.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
可以扩展一下,当网络检测执行时,可将上述的网络信号制作成帧动画,可以实时显示检测效果。