gradle 正在制作GPS定位应用程序,LocationRequest已弃用,不知道如何解决

huwehgph  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(173)

我正在制作一个位置追踪应用。这是我第一次用Java编程,我不知道如何更新过时的方法。我看到Android Studio在尽全力解释如何使用当前的方法,但我还是把它搞砸了。

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应用程序,这是我能表达这个问题的最好方式。
先谢谢你。

ztyzrc3y

ztyzrc3y1#

尝试使用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

相关问题