Android 如何阻止FusedLocationProviderClient返回相同的位置

Android 如何阻止FusedLocationProviderClient返回相同的位置,android,location,fusedlocationproviderclient,Android,Location,Fusedlocationproviderclient,我正在使用FusedLocationProviderClient获取用户当前位置,一切正常,但每次打开应用程序时都会显示我获取的第一个位置,无论手机在哪里 此方法为我提供当前的纬度和经度 private void currentCoordinate() { //getting the user last known location if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.

我正在使用FusedLocationProviderClient获取用户当前位置,一切正常,但每次打开应用程序时都会显示我获取的第一个位置,无论手机在哪里

此方法为我提供当前的纬度和经度

 private void currentCoordinate() {
    //getting the user last known location
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location)                        if (location != null) {
                        System.out.println(location.getLatitude() + "llloction");

                        getCurrentLocation(location.getLatitude(), location.getLongitude());
                    }
                }
            });
}
private void getCurrentLocation(double latitude , double longitude  ) {

    Geocoder gCoder = new Geocoder(this);
    List<Address> addresses;

    try {
        addresses = gCoder.getFromLocation(latitude, longitude, 1);
        if (addresses != null && addresses.size() > 0){
            String stringForFragment = addresses + "";
            String test = stringForFragment;

            //split the address text for the first time
            test = test.
                    substring(test.indexOf("[addressLines=["),test.indexOf("],feature"));
            //split the adress text for the second time
            String finalTest = test.substring(test.indexOf(":") + 1);

            mPlaceAutocompleteFragment.setText(finalTest);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private void currentCoordinate(){
//获取用户最后已知的位置
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
返回;
}
mFusedLocationClient.getLastLocation()文件
.addOnSuccessListener(此,新的OnSuccessListener(){
@凌驾
如果(位置!=null),则在成功(位置)时公共无效{
System.out.println(location.getLatitude()+“llLocation”);
getCurrentLocation(location.getLatitude(),location.getLatitude());
}
}
});
}
这是从经纬度获取地址的方法

 private void currentCoordinate() {
    //getting the user last known location
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location)                        if (location != null) {
                        System.out.println(location.getLatitude() + "llloction");

                        getCurrentLocation(location.getLatitude(), location.getLongitude());
                    }
                }
            });
}
private void getCurrentLocation(double latitude , double longitude  ) {

    Geocoder gCoder = new Geocoder(this);
    List<Address> addresses;

    try {
        addresses = gCoder.getFromLocation(latitude, longitude, 1);
        if (addresses != null && addresses.size() > 0){
            String stringForFragment = addresses + "";
            String test = stringForFragment;

            //split the address text for the first time
            test = test.
                    substring(test.indexOf("[addressLines=["),test.indexOf("],feature"));
            //split the adress text for the second time
            String finalTest = test.substring(test.indexOf(":") + 1);

            mPlaceAutocompleteFragment.setText(finalTest);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private void getCurrentLocation(双纬度、双经度){
地理编码器gCoder=新地理编码器(本);
列出地址;
试一试{
地址=gCoder.getFromLocation(纬度,经度,1);
if(addresses!=null&&addresses.size()>0){
字符串stringForFragment=地址+“”;
字符串测试=stringForFragment;
//第一次拆分地址文本
测试=测试。
子字符串(test.indexOf(“[addressLines=[”),test.indexOf(“],feature”);
//第二次拆分地址文本
字符串finalTest=test.substring(test.indexOf(“:”)+1);
mplaceautompletefragment.setText(finalTest);
}
}捕获(IOE异常){
e、 printStackTrace();
}
}

请改用位置管理器,因为fused location会返回上次保存的位置。@Ibrahim谢谢,我会尝试这样做,但您能否告诉我,在应用程序关闭并在其他地方再次打开后,谁可以保存位置?