0

When I enter a screen, I check for if GPS is turned on, if not, the dialog to enable GPS is shown. When user clicks Yes, onActivityResult -> GPS is turned on and I try to get the location but this always returns null

When I enter the screen with GPS already on, location is retrieved properly. I have been struggling with this for few days now and can't seem to find any resources.

please help me what did i do wrong?

 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recyler_list, container, false);
        initializeView(view);
         registerGoogleClient()
        return view;

    }

    private void registerGoogleClient() {

     try {
                // Create an instance of GoogleAPIClient.
                if (mGoogleApiClient == null) {
                    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .addApi(LocationServices.API)
                            .build();
                    LocationRequest locationRequest = LocationRequest.create();
                    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
                    builder.addLocationRequest(locationRequest);
                    builder.setAlwaysShow(true);
                    locationSettingsRequest = builder.build();
                    connectGoogleApiClient();
                }

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

     @Override
        public void onStart() {
            super.onStart();

            try {
                connectGoogleApiClient();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

        private void connectGoogleApiClient() {
            try {
                if (mGoogleApiClient != null) {
                    mGoogleApiClient.connect();
                    checkDeviceGps();
                }
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

      @Override
        public void onStop() {
            super.onStop();
            disConnectGoogleApiClient();
        }

     private void disConnectGoogleApiClient() {

            try {
                if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.disconnect();
                }
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }


        @Override
        public void onConnected(Bundle connectionHint) {

            try {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    System.out.println("if block");

                    if (getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                            && getActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        // mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                    }
                    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                } else {
                    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                }

                System.out.println("mLastLocation is====>" + mLastLocation);
                if (mLastLocation != null) {
                    latitude = mLastLocation.getLatitude();
                    longtude = mLastLocation.getLongitude();
                    System.out.println("Latitude====>" + mLastLocation.getLatitude());
                    System.out.println("Longitude====>" + mLastLocation.getLongitude());
                }
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }


        private void checkDeviceGps() {

            try {

                PendingResult<LocationSettingsResult> pendingResult =
                        LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, locationSettingsRequest);
                pendingResult.setResultCallback(this);

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

        @Override
        public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
            final Status status = locationSettingsResult.getStatus();
            switch (status.getStatusCode()) {

                case LocationSettingsStatusCodes.SUCCESS:
                    break;

                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    try {
                        status.startResolutionForResult(getActivity(), 100);
                    } catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            switch (requestCode) {

                case 100:
                    getLatLong();
                    break;
            }
        }


        private void getLatLong() {

            try {
             if (mLastLocation != null) {
                    latitude = mLastLocation.getLatitude();
                    longtude = mLastLocation.getLongitude();
                    System.out.println("Latitude====>" + mLastLocation.getLatitude());
                    System.out.println("Longitude====>" + mLastLocation.getLongitude());
                }
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

enter image description here

AbhiRam
  • 2,033
  • 7
  • 41
  • 94

1 Answers1

0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                System.out.println("if block");

                if (getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        && getActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);Log.d("TAG",mLastLocation.getLatitude());


                }

            } else {
                mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            }

try this change , and post what LOG do u get , if u are not getting the LOG, means you dont have required permissions and if u get log but not location,then we can rule out permission issues .But the way what API level are u testing on ?

Ak9637
  • 990
  • 6
  • 12