位置不断更新 android 而不是一次

Location updates constantly in android instead of once

我正在学习如何获取用户位置的教程,问题是我在发送位置时应该只获取一次位置坐标而不是无限次,但我一直在获取位置坐标.

教程小哥只有在发送位置时才获取位置,而不是像我一样经常。 为什么会发生这种情况,我该如何解决?

我只想在用户更改位置时在日志中获取一次位置数据,而不是无限次。请帮助。

我正在使用:- Android 工作室版本:- 4.0.1

AVD:- 像素 2 API 26

这是代码:-

public class MainActivity extends AppCompatActivity {

    LocationManager locationManager;
    LocationListener locationListener;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.i("Location",location.toString());
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
    }
} 

这是来自 android 工作室的 logcat:-

2020-10-16 12:39:46.237 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m56s930ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:46.240 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m56s930ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:47.238 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m57s930ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:47.243 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m57s931ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:48.245 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m58s931ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:48.248 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m58s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:49.238 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m59s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:49.241 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m59s930ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:50.231 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m0s926ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:50.235 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m0s927ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:51.236 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m1s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:51.243 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m1s929ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.237 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m2s929ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.240 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m2s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.273 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.356222,-122.415847 hAcc=20 et=+1d8h28m2s967ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.277 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.356222,-122.415847 hAcc=20 et=+1d8h28m2s967ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:53.237 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.356222,-122.415847 hAcc=20 et=+1d8h28m3s929ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]

没有办法做到这一点。您所能做的就是在其上方编写包装器,并在值更改时发送位置坐标。