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,
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