0

I want to get the location of the device at app startup.

I have been looking through the docs, trying to figure out what goes wrong, but I can't seem to find it. onLocationChanged is never called.

private void getLocation() {
    // Connected to Google API

    // Create location request with minimum interval of a minute, and normal of an hour
    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(PRIORITY_BALANCED_POWER_ACCURACY);
    mLocationRequest.setFastestInterval(1000 * 60);
    mLocationRequest.setInterval(1000 * 60 * 60);

    // Request permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{"ACCESS_FINE_LOCATION"}, MY_PERMISSION_LOCATION_CODE);
        return;
    }

    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSION_LOCATION_CODE: {
            if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                LocationServices.FusedLocationApi.requestLocationUpdates(
                        mGoogleApiClient, mLocationRequest, this);
            }
            else {
                TextView loading = (TextView)findViewById(R.id.connecting_text);
                loading.setText("Please enable Location under Settings > Apps > Beet > Permissions.");
                loading.setTextSize(20);
            }
            break;
        }
    }
}

public void onLocationChanged(Location location) {
    System.out.println("GETLOCATION1 ________________");
    mLastLocation = location;
    System.out.println(location.getLatitude() + " - " + location.getLongitude() + " ________________");
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Bjørn Skagen
  • 433
  • 1
  • 4
  • 5
  • try to debug your code , `onRequestPermissionsResult` is getting called or not and check is permission is getting granted – Pavneet_Singh Dec 17 '16 at 16:43
  • The end of 'getLocation' is reached. – Bjørn Skagen Dec 17 '16 at 17:10
  • try to debug all points in my above comment , if `onRequestPermissionsResult` isn't get called mean something wrong with your permission code but you have to narrow down the issue like it's about permission or location and try to look at logcat too – Pavneet_Singh Dec 17 '16 at 17:11
  • Can't it be that ' if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)' evaluates as true? Seeing as the function reaches the end. The app is already given the required permissions on the emulator. – Bjørn Skagen Dec 17 '16 at 17:13
  • you have to mimic the location on emulator , see this link http://stackoverflow.com/questions/38247350/emulators-location-simulation-not-working and look for similar information – Pavneet_Singh Dec 17 '16 at 17:16

1 Answers1

0

Try getting permission like this: it works in my case

if (!checkIfAlreadyhavePermission()) {
            ActivityCompat.requestPermissions(this, new String[]{"ACCESS_FINE_LOCATION"}, MY_PERMISSION_LOCATION_CODE);
        }

private boolean checkIfAlreadyhavePermission() {
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
    return result == PackageManager.PERMISSION_GRANTED;
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSION_LOCATION_CODE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  Call_location_updates();
            } else {
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.gps_permission), Toast.LENGTH_LONG).show();
                finish();
            }
            break;
        }
        // other 'case' lines to check for other
        // permissions this app might request
    }
}

Also create your LocationRequest object like this:

private final int INTERVAL = 5000;
private final int FAST_INTERVAL = 1000;

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(INTERVAL);
    mLocationRequest.setFastestInterval(FAST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

protected synchronized void buildGoogleApiClient() {

    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();

}



 public void Call_location_updates(){
         Location location = LocationServices.FusedLocationApi
                    .getLastLocation(mGoogleApiClient);
            if (location != null) {
                onLocationChanged(location);
            }
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);

}

 @Override
    public void onLocationChanged(Location location) {
        //do what u want with location
    }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62