Skip to content Skip to sidebar Skip to footer

Android Locationlistener, Abstractmethoderror On Onstatuschanged And Onproviderdisabled

I have a simple activity with a button, that uses the LocationManager to try to get the current location: protected void onCreate(Bundle savedInstanceState) { super.onCreate(sa

Solution 1:

just add these three function at the end of your code:

@Override
public void onProviderEnabled(@NonNull String provider) {

}

@Override
public void onProviderDisabled(@NonNull String provider) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    
}

and write your code that you would like to execute when status changes

Solution 2:

Here's my situation. On Android 9 (API 28) and Android 10 (API 29), in the android.location.LocationManager class there is this method:

privatevoid_handleMessage(Message msg) {
    switch (msg.what) {
        case TYPE_LOCATION_CHANGED:
            Locationlocation=newLocation((Location) msg.obj);
            mListener.onLocationChanged(location);
            break;
        case TYPE_STATUS_CHANGED:
            Bundleb= (Bundle) msg.obj;
            Stringprovider= b.getString("provider");
            intstatus= b.getInt("status");
            Bundleextras= b.getBundle("extras");
            mListener.onStatusChanged(provider, status, extras);
            break;
        case TYPE_PROVIDER_ENABLED:
            mListener.onProviderEnabled((String) msg.obj);
            break;
        case TYPE_PROVIDER_DISABLED:
            mListener.onProviderDisabled((String) msg.obj);
            break;
    }
    locationCallbackFinished();
}

which seems to be called. Also, the difference of implementation for LocationListener between Android 29 and Android 30 is this:

Android29:
publicinterfaceLocationListener {
    voidonLocationChanged(Location location);
    
    @DeprecatedvoidonStatusChanged(String provider, int status, Bundle extras);

    voidonProviderEnabled(String provider);

    voidonProviderDisabled(String provider);
}

Android30:
publicinterfaceLocationListener {
    voidonLocationChanged(@NonNullLocation location);

    @DeprecateddefaultvoidonStatusChanged(String provider, int status, Bundle extras) {}

    defaultvoidonProviderEnabled(@NonNullString provider) {}

    defaultvoidonProviderDisabled(@NonNullString provider) {}
}

It's obvious that the word "default" is the difference. Given that I have the same problem as you, I need to find a workaround now. Or use the response of mohit48. Good luck!

Solution 3:

I was not completely satisfied with the answers above and decided to elaborate a little.

As @sunlover3 rightfully stated, the problematic methods' implementation was changed. Before API 30 they were abstract, which means that if the compileSdk would be anything but 30 the OP's code would give compile error (these methods must have been implemented). But in the API 30 they added empty default methods' implementation, which means that if the compileSdk is 30, the compiler checks these methods and gives no error, they are already implemented.

BUT, if one runs an app with compileSdk 30 on and older OS with the version below 30, these methods' implementation is still old, i.o. there is none, they are abstract! Hence, the error.

Honestly, that's an obvious SDK's devs error, such things must not happen. At worst there should've been a compile-time warning that one should still override these methods for the older versions compatibility. I hope someone less lazy than I'm on this topic will report this to google tracker!

Solution 4:

I faced the same issue and i managed to solve it by overriding the onStatusChanged() as suggested above.

However, the Android team might be working on something and i found that there is this interface that solves the problem without overriding, as far as i have tested.

You can see here the summary for the androidx.core.location package.

In order to use this new classes in your project you should use the latest beta version of the androidx.core. Read here.

Solution 5:

Override the methods in your code. ...

overridefunonProviderDisabled(provider: String) {}
overridefunonProviderEnabled(provider: String) {}
overridefunonStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
...

Post a Comment for "Android Locationlistener, Abstractmethoderror On Onstatuschanged And Onproviderdisabled"