2

enter image description hereI have an issue to my android app, GPS in my app doesn't work well but when i run Google maps and run again my app it runs well. I don't know the reason

public void getDeviceLocation(){

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    try{

    final Task location = mFusedLocationProviderClient.getLastLocation();

        location.addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {

                if(task.isSuccessful() && task.getResult() != null){

                   Location currentLocation = (Location) task.getResult();


                    longitude = currentLocation.getLongitude();
                    latitude  = currentLocation.getLatitude();



                }else{

                    Toast.makeText(MapsActivity.this,"unable to get your current location, please refresh the page or check your internet access is good",Toast.LENGTH_SHORT).show();

                }

            }
        });

    }catch (SecurityException e){
        Log.e(TAG,"Security exception: " + e.getMessage());
    }
}

In the manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

that is the method to make a request to turn on GPS, this code make a dialog pop up and tell him if he accepted to turn on GPS or not

private void displayLocationSettingsRequest(Context context) {

    GoogleApiClient googleApiClient = null;
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API).addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
        googleApiClient.connect();
        LocationRequest locationRequest = LocationRequest.create();

         locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(5 * 1000);
        locationRequest.setFastestInterval(5 * 1000 / 2);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);


        builder.setAlwaysShow(true); 

        PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        toast("Success");
                        // All location settings are satisfied. The client can
                        // initialize location
                        // requests here.
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        toast("GPS is not on");
                        // Location settings are not satisfied. But could be
                        // fixed by showing the user
                        // a dialog.
                        try {
                            // Show the dialog by calling
                            // startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(decision.this, 1000);

                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        toast("Setting change not allowed");
                        // Location settings are not satisfied. However, we have
                        // no way to fix the
                        // settings so we won't show the dialog.
                        break;
                }
            }
        });
    }
}
Hasan
  • 21
  • 5
  • Did you add the permission in the manifest? Has it been accepted by the time you run this code? – NSimon Jun 27 '18 at 14:25
  • yes i add my permissions here it the permissions in my manifast – Hasan Jun 27 '18 at 14:28
  • What do you mean it does not run well? Does it crash? If it does, please post the log. – Deividas Strioga Jun 27 '18 at 14:34
  • And what about the second part of my question? Do you specifically request the permission to the user before running this code? Location is not a permission that is "granted" by default. – NSimon Jun 27 '18 at 14:34
  • 1
    I agree with Strioga. What do you mean by "It doesn't work well well"? What is your actual problem? Are you getting any errors? Is your map camera not updating? Are you having issues with SupportMapFragment, MapFragment, or Mapview. Are using currect methods such as setMyLocationEnabled(true)? Please be more clear. – J. Jefferson Jun 27 '18 at 14:40
  • No, it doesn't crash. when the GPS of mobile is turned off I ask user to turn his GPS, so when he accepted and GPS is turned on, I can not take the location from device – Hasan Jun 27 '18 at 14:41
  • No there is not error in logs – Hasan Jun 27 '18 at 14:42
  • So you're not getting the user's GPS location after GPS is turned back on and the user navigates back to the app? – J. Jefferson Jun 27 '18 at 14:51
  • If the code doesn't request location updates then there won't be location updates, unless some other application requests them. That's why running Google Maps helps. – Markus Kauppinen Jun 27 '18 at 15:05
  • this photo shows what happen when i go to mapsActivity and i cannot get device location https://i.stack.imgur.com/dR3yq.png – Hasan Jun 27 '18 at 15:07
  • I can not understand you Markus – Hasan Jun 27 '18 at 15:10
  • Please checkout how to ask user for location permission: https://stackoverflow.com/a/40142454/6267116 – Parth Bhoiwala Jun 27 '18 at 17:28

0 Answers0