-3

What happens when an app uses Google Play Services Location API to fetch user's location and the GPS is disabled?What code is written to make the app ask the user to turn on the GPS?

Also, what code should be written for versions Marshmallow or greater to ask for permission to access location (as versions greater than API 23 ask for permissions on run time of the app).

Also how to handle if the user does not give the permission?

1 Answers1

1

What happens when an app uses Google Play Services Location API to fetch user's location and the GPS is disabled?What code is written to make the app ask the user to turn on the GPS?

If GPS is disabled, the last known location can be retrieved via Google play services,

  1. Create a GoogleApiClient

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

Get the last known location like this,

LocationServices.FusedLocationApi
                .getLastLocation(mGoogleApiClient);

https://developer.android.com/training/location/retrieve-current.html

Also, what code should be written for versions Marshmallow or greater to ask for permission to access location (as versions greater than API 23 ask for permissions on run time of the app).

Also how to handle if the user does not give the permission?

From Android Marshmallow, it is necessary to request permission at runtime.Here is the link for it,

https://developer.android.com/training/permissions/requesting.html

GeekDroid
  • 1,634
  • 3
  • 16
  • 36
  • @AayushTaneja https://stackoverflow.com/questions/843675/how-do-i-find-out-if-the-gps-of-an-android-device-is-enabled – GeekDroid Aug 22 '17 at 12:30
  • No, im not asking for the last location. Im asking how to ask the user to turn on the GPS if it is off?@BatCat – Aayush Taneja Aug 22 '17 at 12:42