Android 获取片段中的当前纬度和经度

Android 获取片段中的当前纬度和经度,android,gps,currentlocation,Android,Gps,Currentlocation,我试图在一个片段中获取我当前的GPS坐标,但它不起作用。这是我的密码: public class AjouterBancFragment extends Fragment { TextView textViewLongLat; LocationManager locationManager; String provider; Location location; @Nullable @Override public View onCre

我试图在一个片段中获取我当前的GPS坐标,但它不起作用。这是我的密码:

public class AjouterBancFragment extends Fragment {

    TextView textViewLongLat;
    LocationManager locationManager;
    String provider;
    Location location;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.ajouter_banc, container, false);

        textViewLongLat = v.findViewById(R.id.textViewLongLat);

        // Déclarations
            // Bouton
            Button buttonAdd = v.findViewById(R.id.buttonAjouterBanc);

            buttonAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    locationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
                    Criteria c=new Criteria();
                    //if we pass false than
                    //it will check first satellite location than Internet and than Sim Network
                    provider=locationManager.getBestProvider(c, false);
                    if ((ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
                        location=locationManager.getLastKnownLocation(provider);
                    }

                    if(location!=null)
                    {
                        double lng=location.getLongitude();
                        double lat=location.getLatitude();
                        textViewLongLat.setText("Position actuelle : "+lat+","+lng);
                    }
                    else
                    {
                        textViewLongLat.setText("No Provider");
                    }
                }
            });
        return v;
    }
}
问题是这段代码给出了我2天前所在位置的坐标。
谢谢。

不要使用getLastKnownLocation。使用requestSingleUpdate,它将打开您的GPS并在向您发送回调之前找到您的当前位置。

我尝试了另一种方法,因为我不太了解“requestSingleUpdate”的工作原理 所以我看了一些使用“requestLocationUpdates”的教程,但问题是它也不起作用。 这是我的新代码:

// All import stuffs

public class AjouterBancFragment extends Fragment {

    TextView textViewLongLat;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.ajouter_banc, container, false);

        textViewLongLat = v.findViewById(R.id.textViewLongLat);
        Button buttonAdd = v.findViewById(R.id.buttonAjouterBanc);

        buttonAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
                    LocationListener locationListener = new AjouterBancFragment.MyLocationListener();
                    if ((ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
                        Toast.makeText(getContext(), "OK", Toast.LENGTH_SHORT).show();   // Toast is showing
                    }

                }
            });
        return v;
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
            Toast.makeText(getContext(), "OnLocationChanged", Toast.LENGTH_SHORT).show();   // Toast not showing
            String longitude = "Longitude: " + loc.getLongitude();
            String latitude = "Latitude: " + loc.getLatitude();
            String s = longitude + "\n" + latitude;
            textViewLongLat.setText(s);
        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(getContext(), "onProviderDisabled", Toast.LENGTH_SHORT).show();  // Toast not showing
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(getContext(), "on ProviderEnabled", Toast.LENGTH_SHORT).show();  // Toast not showing
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(getContext(), "onStatusChanged", Toast.LENGTH_SHORT).show();  // Toast not showing
        }
    }
}
我放置Toast消息是为了查看代码的步骤,但我只有一条Toast消息出现(它在代码中被注释)。如果有人知道为什么它不起作用,我很感兴趣! 谢谢你