'How to get the cellular signal strength with the GPS disabled?

I get the signal strength this way :

TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();

However I noticed that it works ONLY IF the permission is granted and the GPS is enabled.

Maybe I'm doing it wrong, is there another way to get the cell signal strength without having to turn the GPS on?

I tried that way too :

@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= 31) {
    this.telephonyManager?.registerTelephonyCallback(
        this.signalLevelService,
        object : TelephonyCallback(), TelephonyCallback.SignalStrengthsListener {
            override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
                Log.i("Cell Signal", "${signalStrength.level}")
            }
        })
} 

else {
    this.telephonyManager?.listen(object : PhoneStateListener() {
        @Deprecated("Deprecated in Java")
        override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
            Log.i("Cell Signal", "${signalStrength.level}")
        }
    }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
}

But the result is the same ... The GPS must be enabled for this to work.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source