Android,LocationListener.onStatusChanged已被弃用,有什么可以替代它的吗?

4
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已被弃用。官方推荐使用Google Location Services API,但这仅适用于设备拥有Google服务的情况。不幸的是,我的设备没有安装Google服务,有其他替代方案吗?


2
不需要任何操作。他们已经弃用了OUT_OF_SERVICE和TEMPORARILY_OUT_OF_SERVICE状态。它们将永远不会发生。由于这种变化,该函数被弃用,因此它永远不会被调用。 - Gabe Sechan
1个回答

1

我在 Android 10 以下的设备上遇到了这个问题。

以下是堆栈跟踪信息;

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)

不幸的是,它没有引用我的任何源代码,但很明显它源自位置监听器。

查看此处的文档,上面写着:

onStatusChanged(String provider, int status, Bundle extras)

此方法已在API级别29中弃用。 此回调将永远不会在Android Q及以上版本上调用。

最初的实现方式是这样的:

     val listener = LocationListener {
                         trySendBlocking(it)
                    }

将其更改为下面的代码片段会在super.onStatusChanged中添加删除线,但仍会崩溃,因为文档说它在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)
                    }
        
                }

移除 super.onStatusChanged(provider, status, extras) 但保留重写,可确保该调用不会传播到父抽象类中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接