有时候我们需要实时监听手机的网络及其它的信号值,但是菜鸟的我在网上百度了很久,没有找到比较合适的例子,后面直接看系统的API,完成了下面的例子,在这做一下笔记:
public static void getPhoneState(Context context) {
final TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener MyPhoneListener = new PhoneStateListener() {
@Override
//获取对应网络的ID,这个方法在这个程序中没什么用处
public void onCellLocationChanged(CellLocation location) {
if (location instanceof GsmCellLocation) {
int CID = ((GsmCellLocation) location).getCid();
} else if (location instanceof CdmaCellLocation) {
int ID = ((CdmaCellLocation) location).getBaseStationId();
}
}
//系统自带的服务监听器,实时监听网络状态
@Override
public void onServiceStateChanged(ServiceState serviceState) {
super.onServiceStateChanged(serviceState);
}
//这个是我们的主角,就是获取对应网络信号强度
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
//这个ltedbm 是4G信号的值
String signalinfo = signalStrength.toString();
String[] parts = signalinfo.split(" ");
int ltedbm = Integer.parseInt(parts[9]);
//这个dbm 是2G和3G信号的值
int asu = signalStrength.getGsmSignalStrength();
int dbm = -113 + 2 * asu;
if (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE) {
Log.i("NetWorkUtil", "网络:LTE 信号强度:" + ltedbm + "======Detail:" + signalinfo);
} else if (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA ||
telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSPA ||
telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSUPA ||
telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) {
String bin;
if (dbm > -75) {
bin = "网络很好";
} else if (dbm > -85) {
bin = "网络不错";
} else if (dbm > -95) {
bin = "网络还行";
} else if (dbm > -100) {
bin = "网络很差";
} else {
bin = "网络错误";
}
Log.i("NetWorkUtil", "网络:WCDMA 信号值:" + dbm + "========强度:" + bin + "======Detail:" + signalinfo);
} else {
String bin;
if (asu < 0 || asu >= 99) bin = "网络错误";
else if (asu >= 16) bin = "网络很好";
else if (asu >= 8) bin = "网络不错";
else if (asu >= 4) bin = "网络还行";
else bin = "网络很差";
Log.i("NetWorkUtil", "网络:GSM 信号值:" + dbm + "========强度:" + bin + "======Detail:" + signalinfo);
}
super.onSignalStrengthsChanged(signalStrength);
}
};
telephonyManager.listen(MyPhoneListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}