-3

When I try to return two values from the method below to apply into another variable it always return 0.0. What's my problem? Here is my code:

    public double[] getLocation() {
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
    }else{
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null){
            double latti = location.getLatitude();
            double longi = location.getLongitude();

            this.latti = latti;
            this.longi = longi;


        }


    }
    return new double[]{latti,longi};


}

This is my method, and i want to apply those two values in here

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

    switch (requestCode){
        case REQUEST_LOCATION:getLocation();
            break;
    }
}

This is onRequestionPermission method

My rest of the code

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
double latti;
double longi;
LocationManager locationManager;
static final int REQUEST_LOCATION = 1;

 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    double[] curLocation = getLocation();
    double latitude = curLocation[0];
    double longtitude = curLocation[1];
          Toast.makeText(this, "latitude: " + latitude + "longitude: " + longtitude, Toast.LENGTH_SHORT).show();

}

but seems always return back 0.0 and 0.0 ...

BenMorel
  • 34,448
  • 50
  • 182
  • 322
GAME TTXD
  • 151
  • 10

1 Answers1

0

Override onRequestPermissionsResult() and check whether the permissions are given or not, if yes move your else code here.

@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
    switch (requestCode) {
    case REQUEST_LOCATION: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay!
            Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null) {
        double latti = location.getLatitude();
        double longi = location.getLongitude();

        this.latti = latti;
        this.longi = longi;
        }

        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
        }
    }
}
Archana
  • 597
  • 4
  • 18