Enable The GPS Programmatically
Here is a simple way to turn on gps programatically
private void askForGPs() {
LocationRequest locationRequest;
locationRequest = locationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000); // the fastest rate in milliseconds at which your app can handle location updates
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.d(TAG, "success");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
//this will ask user to enable gps
try {
Log.d("TAG", "required");
//This line prompt a pop to enable gps
status.startResolutionForResult(getActivity(), GPS_SETTINGS);
} catch (IntentSender.SendIntentException e) {
}
Log.d(TAG, "required");
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.d(TAG, "unavailable");
break;
}
}
});
}
private void askForGPs() {
LocationRequest locationRequest;
locationRequest = locationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000); // the fastest rate in milliseconds at which your app can handle location updates
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.d(TAG, "success");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
//this will ask user to enable gps
try {
Log.d("TAG", "required");
//This line prompt a pop to enable gps
status.startResolutionForResult(getActivity(), GPS_SETTINGS);
} catch (IntentSender.SendIntentException e) {
}
Log.d(TAG, "required");
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.d(TAG, "unavailable");
break;
}
}
});
}
No comments :
Post a Comment