Android, LocationListener.onStatusChanged deprecated,What can replace it

996 Views Asked by At
onStatusChanged
Added in API level 1
Deprecated in API level 29

public void onStatusChanged (String provider, 
                int status, 
                Bundle extras)
This method was deprecated in API level 29.

This callback will never be invoked on Android Q and above.
This callback will never be invoked on Android Q and above, and providers can be considered as always in the LocationProvider#AVAILABLE state.

Note that this method only has a default implementation on Android R and above, and this method must still be overridden in order to run successfully on Android versions below R. LocationListenerCompat from the compat libraries may be used to avoid the need to override for older platforms.

onStatusChanged deprecated The Google Location Services API is officially recommended, but this can only be used if the device has Google Services. Unfortunately, my device does not have Google Services, so is there any other alternative?

1

There are 1 best solutions below

0
KE Keronei On

I encountered this issue on devices below Android 10.

Here is the stack trace;

Fatal Exception: java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onStatusChanged(java.lang.String, int, android.os.Bundle)"
       at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:299)
       at android.location.LocationManager$ListenerTransport.-wrap0()
       at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:237)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6548)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)

The unfortunate thing is, it didn't have a reference to any of my source, but it is evident it stems from location listener.

Looking through the docs here , it says:

onStatusChanged(String provider, int status, Bundle extras)

This method was deprecated in API level 29. This callback will never be invoked on Android Q and above.

The initial implementation was this way:

     val listener = LocationListener {
                         trySendBlocking(it)
                    }

Changing it to the snippet below adds a strikethrough to the super.onStatusChanged and still crashes because the docs says it was deprecated in API 29

         val listener = object : LocationListener {
                    override fun onLocationChanged(p0: Location) {
                       // handle location here
                    }
        
                    override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
                       // Remove this line
                        super.onStatusChanged(provider, status, extras)
                    }
        
                }

Removing super.onStatusChanged(provider, status, extras) but leaving the overide ensures that the call is not propagated into the parent abstract class.