Domanda

My Android app monitors cellular signal strengths. On CDMA devices, it works without any problems. On many GSM devices, it works without any problems. However, on some GSM devices, I am getting force closes with the following error message:

java.lang.ClassCastException: android.telephony.cdma.CdmaCellLocation cannot be cast to android.telephony.gsm.GsmCellLocation

I am fairly certain that this issue occurs on some Samsung devices in the US when they receive a 4G LTE signal; I believe it occurs on other devices and countries as well. I am still trying to determine the exact service providers and devices involved.

Here is the relevant snippet of code from MyService.java; I have marked the line referenced by the error message with //********:

public void onCreate() {
    tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    ...
}

public int onStartCommand(Intent in, int flags, int startId) {
    signalStrengthListener = new SignalStrengthListener();
    ((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS | SignalStrengthListener.LISTEN_SERVICE_STATE | SignalStrengthListener.LISTEN_DATA_CONNECTION_STATE | SignalStrengthListener.LISTEN_CELL_LOCATION);
    return START_STICKY;
    ...
}

public void onSignalStrengthsChanged(SignalStrength signalStrength) {
    isGSM = signalStrength.isGsm();
    if (isGSM == true) {
        GCellLoc = (GsmCellLocation)tm.getCellLocation();    //********
    ...
}

Not sure why is anything from android.telephony.cdma is appearing if this code should only be triggered when isGSM returns true. Perhaps when an LTE signal is detected, that puts the phone in some sort of dual CDMA/GSM mode as far as the Android API is concerned? I have not been able to find any documentation of similar behavior. How can I handle this in my code? Thanks.

È stato utile?

Soluzione

I cannot explain why it is happening. I have some theories, but you have similar ones.

While you sort out the reason, you can deal with issue pragmatically. You know that sometimes even though you are getting a GSM SignalStrength in your callback, sometimes on some devices you get a CDMA CellLocation from TelephonyManager and write your code to handle that case using instanceOf instead of relying on .isGSM() from the SignalStrength.

CellLocation cellLoc = tm.getCellLocation();
if(cellLoc instanceof GsmCellLocation) {
   GCellLoc = (GsmCellLocation) cellLoc;
   // do work 
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top