TelephonyManager.getAllCellInfo() return Null 或显示 Nothing
TelephonyManager.getAllCellInfo() return Null or displays Nothing
我遇到了 getAllCellInfo()
的问题。
应用需要权限:
这是我的代码:
1- 监听器
private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
2- 来自信号强度的信息
TextView comparisonText = (TextView) findViewById(R.id.textViewComparison);
Object ssFieldValueRsrp = null;
Object ssFieldValueRsrq = null;
Object ssFieldValueRssnr = null;
Object ssFieldValueCqi=null;
try {
Field privateStringSsFieldRSRQ = SignalStrength.class.getDeclaredField("mLteRsrq");
Field privateStringSsFieldRSRP = SignalStrength.class.getDeclaredField("mLteRsrp");
Field privateStringSsFieldRssnr = SignalStrength.class.getDeclaredField("mLteRssnr");
Field privateStringSsFieldCqi = SignalStrength.class.getDeclaredField("mLteCqi");
privateStringSsFieldRSRQ.setAccessible(true);
ssFieldValueRsrq = privateStringSsFieldRSRQ.get(signalStrength);
privateStringSsFieldRSRP.setAccessible(true);
ssFieldValueRsrp = privateStringSsFieldRSRP.get(signalStrength);
privateStringSsFieldRssnr.setAccessible(true);
ssFieldValueRssnr = privateStringSsFieldRssnr.get(signalStrength);
privateStringSsFieldCqi.setAccessible(true);
ssFieldValueCqi = privateStringSsFieldCqi.get(signalStrength);
} catch (NoSuchFieldException ex) {
} catch (IllegalAccessException x) {
}
String ssRsrp = Integer.toString((int) ssFieldValueRsrp);
String ssRsrq = Integer.toString((int) ssFieldValueRsrq);
String ssRssnr = Integer.toString((int) ssFieldValueRssnr);
String ssCqi = Integer.toString((int) ssFieldValueCqi);
String headerString = "Info from \"SignalStrength\":";
SpannableString spannableHeaderString = new SpannableString(headerString);
spannableHeaderString.setSpan(new UnderlineSpan(), 0, spannableHeaderString.length(), 0);
comparisonText.setText(spannableHeaderString);
comparisonText.append
(
"\nRSRP: " + ssRsrp
+ "\nRSRQ: " + ssRsrq
+"\nCQI: "+ ssCqi
+"\nRSSNR: "+ ssRssnr
);
3- 来自 CellSignalStrengthLte 的信息
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
List<android.telephony.CellInfo> infor = tm.getAllCellInfo();
for (android.telephony.CellInfo info : infor)
{
if (info instanceof CellInfoLte)
{
CellSignalStrengthLte ss = ((CellInfoLte) info).getCellSignalStrength();
//theButton.setText( ss.toString());
Object fieldValueRSRP = null;
Object fieldValueRSRQ = null;
Object fieldValueRssnr = null;
Object fieldValueCqi=null;
try
{
Field privateStringFieldRSRQ = CellSignalStrengthLte.class.getDeclaredField("mRsrq");
Field privateStringFieldRSRP = CellSignalStrengthLte.class.getDeclaredField("mRsrp");
Field privateStringFieldCqi = CellSignalStrengthLte.class.getDeclaredField("mCqi");
Field privateStringFieldRSSNR = CellSignalStrengthLte.class.getDeclaredField("mRssnr");
privateStringFieldRSRQ.setAccessible(true);
fieldValueRSRQ = privateStringFieldRSRQ.get(ss);
privateStringFieldRSRP.setAccessible(true);
fieldValueRSRP = privateStringFieldRSRP.get(ss);
privateStringFieldRSSNR.setAccessible(true);
fieldValueRssnr = privateStringFieldRSSNR.get(ss);
privateStringFieldCqi.setAccessible(true);
fieldValueCqi = privateStringFieldCqi.get(ss);
}
catch (NoSuchFieldException ex) {}
catch (IllegalAccessException x) {}
String rsrp = Integer.toString((int) fieldValueRSRP);
String rsrq = Integer.toString((int) fieldValueRSRQ);
String rssnr = Integer.toString((int) fieldValueRssnr);
String cqi = Integer.toString((int) fieldValueCqi);
headerString = "Info from \"CellSignalStrengthLte\":";
spannableHeaderString = new SpannableString(headerString);
spannableHeaderString.setSpan( new UnderlineSpan(), 0, spannableHeaderString.length(), 0);
theText.setText
(
"\nAltitude: " + loc.getAltitude() + "\n\n"+loc.getLongitude()+"\n\n"+loc.getLatitude()+"\n\n"
);
theText.append(spannableHeaderString);
theText.append
(
"\nRSRP: " + rsrp
+ "\nRSRQ: " + rsrq
+ "\nCQI: " + cqi
+ "\nRSSNR: " + rssnr
);
}
}
华为Y6II:棉花糖(android6):
应用程序运行顺利但功能被跳过
华为Nova3i (android 9)
应用程序因空指针 在 List<android.telephony.CellInfo> infor = tm.getAllCellInfo();
上崩溃
三星 S10 (android 9)
应用运行宁完美
已解决:
此问题与部分双卡手机有关
我遇到了 getAllCellInfo()
的问题。
应用需要权限:
这是我的代码:
1- 监听器
private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
2- 来自信号强度的信息
TextView comparisonText = (TextView) findViewById(R.id.textViewComparison);
Object ssFieldValueRsrp = null;
Object ssFieldValueRsrq = null;
Object ssFieldValueRssnr = null;
Object ssFieldValueCqi=null;
try {
Field privateStringSsFieldRSRQ = SignalStrength.class.getDeclaredField("mLteRsrq");
Field privateStringSsFieldRSRP = SignalStrength.class.getDeclaredField("mLteRsrp");
Field privateStringSsFieldRssnr = SignalStrength.class.getDeclaredField("mLteRssnr");
Field privateStringSsFieldCqi = SignalStrength.class.getDeclaredField("mLteCqi");
privateStringSsFieldRSRQ.setAccessible(true);
ssFieldValueRsrq = privateStringSsFieldRSRQ.get(signalStrength);
privateStringSsFieldRSRP.setAccessible(true);
ssFieldValueRsrp = privateStringSsFieldRSRP.get(signalStrength);
privateStringSsFieldRssnr.setAccessible(true);
ssFieldValueRssnr = privateStringSsFieldRssnr.get(signalStrength);
privateStringSsFieldCqi.setAccessible(true);
ssFieldValueCqi = privateStringSsFieldCqi.get(signalStrength);
} catch (NoSuchFieldException ex) {
} catch (IllegalAccessException x) {
}
String ssRsrp = Integer.toString((int) ssFieldValueRsrp);
String ssRsrq = Integer.toString((int) ssFieldValueRsrq);
String ssRssnr = Integer.toString((int) ssFieldValueRssnr);
String ssCqi = Integer.toString((int) ssFieldValueCqi);
String headerString = "Info from \"SignalStrength\":";
SpannableString spannableHeaderString = new SpannableString(headerString);
spannableHeaderString.setSpan(new UnderlineSpan(), 0, spannableHeaderString.length(), 0);
comparisonText.setText(spannableHeaderString);
comparisonText.append
(
"\nRSRP: " + ssRsrp
+ "\nRSRQ: " + ssRsrq
+"\nCQI: "+ ssCqi
+"\nRSSNR: "+ ssRssnr
);
3- 来自 CellSignalStrengthLte 的信息
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
List<android.telephony.CellInfo> infor = tm.getAllCellInfo();
for (android.telephony.CellInfo info : infor)
{
if (info instanceof CellInfoLte)
{
CellSignalStrengthLte ss = ((CellInfoLte) info).getCellSignalStrength();
//theButton.setText( ss.toString());
Object fieldValueRSRP = null;
Object fieldValueRSRQ = null;
Object fieldValueRssnr = null;
Object fieldValueCqi=null;
try
{
Field privateStringFieldRSRQ = CellSignalStrengthLte.class.getDeclaredField("mRsrq");
Field privateStringFieldRSRP = CellSignalStrengthLte.class.getDeclaredField("mRsrp");
Field privateStringFieldCqi = CellSignalStrengthLte.class.getDeclaredField("mCqi");
Field privateStringFieldRSSNR = CellSignalStrengthLte.class.getDeclaredField("mRssnr");
privateStringFieldRSRQ.setAccessible(true);
fieldValueRSRQ = privateStringFieldRSRQ.get(ss);
privateStringFieldRSRP.setAccessible(true);
fieldValueRSRP = privateStringFieldRSRP.get(ss);
privateStringFieldRSSNR.setAccessible(true);
fieldValueRssnr = privateStringFieldRSSNR.get(ss);
privateStringFieldCqi.setAccessible(true);
fieldValueCqi = privateStringFieldCqi.get(ss);
}
catch (NoSuchFieldException ex) {}
catch (IllegalAccessException x) {}
String rsrp = Integer.toString((int) fieldValueRSRP);
String rsrq = Integer.toString((int) fieldValueRSRQ);
String rssnr = Integer.toString((int) fieldValueRssnr);
String cqi = Integer.toString((int) fieldValueCqi);
headerString = "Info from \"CellSignalStrengthLte\":";
spannableHeaderString = new SpannableString(headerString);
spannableHeaderString.setSpan( new UnderlineSpan(), 0, spannableHeaderString.length(), 0);
theText.setText
(
"\nAltitude: " + loc.getAltitude() + "\n\n"+loc.getLongitude()+"\n\n"+loc.getLatitude()+"\n\n"
);
theText.append(spannableHeaderString);
theText.append
(
"\nRSRP: " + rsrp
+ "\nRSRQ: " + rsrq
+ "\nCQI: " + cqi
+ "\nRSSNR: " + rssnr
);
}
}
华为Y6II:棉花糖(android6):
应用程序运行顺利但功能被跳过
华为Nova3i (android 9)
应用程序因空指针 在 List<android.telephony.CellInfo> infor = tm.getAllCellInfo();
三星 S10 (android 9)
应用运行宁完美
已解决:
此问题与部分双卡手机有关