Use PhoneStateListener:
PhoneStateListener _psl = new PhoneStateListener() {
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
Log.v("DEBUG_JOB", "onSignalStrengthsChanged");
getLteDbm(signalStrength);
}
};
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(_psl, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
There are three methods that I found:
First: When Build.VERSION.SDK_INT >= 17, use getAllCellInfo() methods of TelephonyManager class. But on some mobile device, getAllCellInfo() always return null.
public int getLteDbm(SignalStrength signalStrength) {
int rsrp = 0;
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
List<CellInfo> mCellInfo = telephonyManager.getAllCellInfo();
if (mCellInfo != null && mCellInfo.size() != 0) {
int index = 0;
for (CellInfo cellInfo : mCellInfo) {
if (cellInfo.isRegistered() && cellInfo instanceof CellInfoLte) {
Log.v("DEBUG_JOB", "[" + index + "]==CellInfoLte");
CellInfoLte x = (CellInfoLte) cellInfo;
CellSignalStrengthLte cstr = x.getCellSignalStrength();
if (cstr != null) {
rsrp = cstr.getDbm();
Log.v("DEBUG_JOB", "RSRP=" + rsrp);
}
CellIdentityLte cellid = x.getCellIdentity();
int pci = cellid.getPci();
Log.v("DEBUG_JOB", "PCI=" + pci);
Log.v("DEBUG_JOB", "CI=" + cellid.getCi());
}
index++;
}
}
return rsrp;
}
======================================================================
Second: Use split string on SignalStrength to get mLteRsrp value. But different mobile device got different position of mLteRsrp value.
public int getLteDbm(SignalStrength signalStrength) {
String ssignal = signalStrength.toString();
String[] parts = ssignal.split(" ");
int rsrp = parts.length >= 9 ? Integer.parseInt(parts[9]) : -1; // some devices is 11
return rsrp;
}
======================================================================
Last: Use the hidden getLteRsrp() method of the SignalStrength class.
public int getLteDbm(SignalStrength signalStrength) {
int rsrp = 0;
try {
rsrp = (Integer) signalStrength.getClass().getMethod("getLteRsrp").invoke(signalStrength);
} catch (Exception ex) {
Log.v("Error", "getLteDbm=" + ex.getMessage());
}
return rsrp;
}
沒有留言:
發佈留言