增加网络信号强度
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
package com.mogo.utils.network.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Build;
|
||||
import android.telephony.PhoneStateListener;
|
||||
import android.telephony.SignalStrength;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.utils.CommonUtils;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* @author liujing
|
||||
* @description 网络状态监听
|
||||
@@ -16,24 +25,72 @@ public class NetworkStatusUtil {
|
||||
|
||||
private static PhoneStatListener phoneStatListener;
|
||||
private static int mSignalStrength;
|
||||
private static SignalStrength signal;
|
||||
|
||||
/**
|
||||
* 监听网络强度
|
||||
*/
|
||||
public static int networkState(Context context) {
|
||||
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
if (telephonyManager == null) {
|
||||
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
public static String networkState(Context context) {
|
||||
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
|
||||
if (info != null && info.isAvailable()) {
|
||||
String netWorkStatus = CommonUtils.getNetworkType(context);
|
||||
|
||||
switch (info.getType()) {
|
||||
case ConnectivityManager.TYPE_WIFI:
|
||||
//wifi
|
||||
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
||||
WifiInfo connectionInfo = manager.getConnectionInfo();
|
||||
int rsSi = connectionInfo.getRssi();
|
||||
return String.valueOf(rsSi);
|
||||
case ConnectivityManager.TYPE_MOBILE:
|
||||
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);
|
||||
//移动网络,可以通过TelephonyManager来获取具体细化的网络类型
|
||||
if (netWorkStatus != "UNKNOWN") {
|
||||
try {
|
||||
if (signal == null) {
|
||||
Log.e("CheckActivity", "getNetWorkInfo: signal为空");
|
||||
break;
|
||||
}
|
||||
Method method = signal.getClass().getMethod("getDbm");
|
||||
mSignalStrength = (int) method.invoke(signal);
|
||||
if (mSignalStrength > -90) {
|
||||
Log.e("CheckActivity", "getNetWorkInfo: 信号强度强");
|
||||
} else {
|
||||
Log.e("CheckActivity", "getNetWorkInfo: 信号强度弱");
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} else {
|
||||
if (signal == null) {
|
||||
break;
|
||||
}
|
||||
mSignalStrength = signal.getGsmSignalStrength();
|
||||
}
|
||||
return String.valueOf(mSignalStrength);
|
||||
}
|
||||
} else {
|
||||
return "UNKNOWN";
|
||||
}
|
||||
if (phoneStatListener == null) {
|
||||
phoneStatListener = new PhoneStatListener();
|
||||
}
|
||||
telephonyManager.listen(phoneStatListener, PhoneStatListener.LISTEN_SIGNAL_STRENGTHS);
|
||||
return mSignalStrength;
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听网络信号的强度变化
|
||||
* 监听非WiFi网络信号强度变化
|
||||
*/
|
||||
static class PhoneStatListener extends PhoneStateListener {
|
||||
@Override
|
||||
|
||||
@@ -35,7 +35,7 @@ public class CheckActivity extends AppCompatActivity {
|
||||
private ArrayList dataArrayList = new ArrayList();
|
||||
private CheckItemInfo mCheckItemInfo = new CheckItemInfo();
|
||||
private Context context;
|
||||
private static int mSignalStrength;
|
||||
private static String mSignalStrength;
|
||||
private Button mButton;
|
||||
|
||||
@Override
|
||||
@@ -111,8 +111,12 @@ public class CheckActivity extends AppCompatActivity {
|
||||
//网络类型
|
||||
String networkType = CommonUtils.getNetworkType(context);
|
||||
//网络强度
|
||||
mSignalStrength = NetworkStatusUtil.networkState(context);
|
||||
Log.d(TAG, "网络类型:" + networkType + "网络强度:" + mSignalStrength);
|
||||
if (mSignalStrength != "UNKNOWN") {
|
||||
mSignalStrength = NetworkStatusUtil.networkState(context);
|
||||
Log.d(TAG, "网络类型:" + networkType + "网络强度:" + mSignalStrength);
|
||||
} else {
|
||||
Log.d(TAG, "网络未连接");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user