添加网络监控
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.mogo.utils.network.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.telephony.PhoneStateListener;
|
||||
import android.telephony.SignalStrength;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* @author liujing
|
||||
* @description 网络状态监听
|
||||
* @since: 7/29/21
|
||||
*/
|
||||
public class NetworkStatusUtil {
|
||||
|
||||
private static PhoneStatListener phoneStatListener;
|
||||
private static int mSignalStrength;
|
||||
|
||||
/**
|
||||
* 监听网络强度
|
||||
*/
|
||||
public static int networkState(Context context) {
|
||||
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
if (telephonyManager == null) {
|
||||
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
}
|
||||
if (phoneStatListener == null) {
|
||||
phoneStatListener = new PhoneStatListener();
|
||||
}
|
||||
telephonyManager.listen(phoneStatListener, PhoneStatListener.LISTEN_SIGNAL_STRENGTHS);
|
||||
return mSignalStrength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听网络信号的强度变化
|
||||
*/
|
||||
static class PhoneStatListener extends PhoneStateListener {
|
||||
@Override
|
||||
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
|
||||
//获取信号强度变化
|
||||
super.onSignalStrengthsChanged(signalStrength);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
mSignalStrength = signalStrength.getLevel();
|
||||
return;
|
||||
}
|
||||
mSignalStrength = signalStrength.getGsmSignalStrength();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user