android中的位置不断更新,而不是一次

android中的位置不断更新,而不是一次,android,gps,locationmanager,android-gps,locationlistener,Android,Gps,Locationmanager,Android Gps,Locationlistener,我在学习如何获取用户位置的教程,问题是我应该只获取一次位置坐标,而不是在发送位置时无限次地获取位置坐标,但我一直在获取位置坐标 指导人员只有在发送位置信息时才获得位置信息,而不是像我一样经常发送位置信息。 为什么会发生这种情况?我如何解决 我只想在用户更改位置时在日志中获取一次位置数据,而不是无限次。请帮忙 我正在使用:- Android Studio版本:-4.0.1 AVD:-像素2 API 26 代码如下:- public class MainActivity extends AppCom

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

指导人员只有在发送位置信息时才获得位置信息,而不是像我一样经常发送位置信息。 为什么会发生这种情况?我如何解决

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

我正在使用:- Android Studio版本:-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 studio的日志:-

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]}]

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

这不是问题,这是标准行为