我在做一个位置跟踪应用。这是我第一次用Java编程,我不知道如何更新不推荐的方法。我看到Android正在尽力解释如何使用当前的方法,但我仍然不断地搞砸它。
LocationRequest locationRequest;
locationRequest = new LocationRequest(); // LocationRequest() is deprecated
// How often does the default location check occur?
locationRequest.setInterval(1000 * DEFAULT_UPDATE_INTERVAL); //.setInterval() is deprecated
// How often does the location check occur when set to the most frequent update?
locationRequest.setFastestInterval(1000 * FAST_UPDATE_INTERVAL); // setFastestInterval is deprecated
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
// setPriority() is deprecated
// PRIORITY_BALANCED_POWER_ACCURACY constant is deprecated
sw_gps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sw_gps.isChecked()) {
// most accurate - use GPS
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// .setPriority method deprecated and PRIORITY_HIGH_ACCURACY constant deprecated
tv_sensor.setText("Using GPS sensors");
} else {
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
tv_sensor.setText("Using Towers + WiFi");
// .setPriority and PRIORITY_BALANCED_POWER_ACCURACY are deprecated
}
}
});
那我该怎么解决这个问题?在setPriority上徘徊时得到的错误消息和常量是:
This method is deprecated. Use LocationRequest.Builder.setIntervalMillis(long) instead. May be removed in a future release.
This constant is deprecated. Use Priority.PRIORITY_HIGH_ACCURACY instead.
This constant is deprecated. Use Priority.PRIORITY_BALANCED_POWER_ACCURACY instead.
如果这不是很容易读的话我很抱歉。这是我第一次使用Java构建android应用程序,这是我提出这个问题的最好方法。
提前谢谢。
发布于 2022-11-29 10:22:30
尝试使用LocationRequest.Builder
下面是创建位置请求的代码
Kotlin
LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
.apply {
setWaitForAccurateLocation(false)
setMinUpdateIntervalMillis(IMPLICIT_MIN_UPDATE_INTERVAL)
setMaxUpdateDelayMillis(100000)
}.build()
Java
new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
.setWaitForAccurateLocation(false)
.setMinUpdateIntervalMillis(IMPLICIT_MIN_UPDATE_INTERVAL)
.setMaxUpdateDelayMillis(100000)
.build()
在这里阅读更多关于https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.Builder的信息
https://stackoverflow.com/questions/74091434
复制相似问题