Created
July 27, 2020 05:12
-
-
Save Dmuasya/fc58993b49476851d0f168d61053e113 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void enableLoc() { | |
if (googleApiClient == null) { | |
googleApiClient = new GoogleApiClient.Builder(MapsActivity.this) | |
.addApi(LocationServices.API) | |
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { | |
@Override | |
public void onConnected(Bundle bundle) { | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
googleApiClient.connect(); | |
} | |
}) | |
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
Log.d("Location error","Location error " + connectionResult.getErrorCode()); | |
} | |
}).build(); | |
googleApiClient.connect(); | |
LocationRequest locationRequest = LocationRequest.create(); | |
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
locationRequest.setInterval(30 * 1000); | |
locationRequest.setFastestInterval(5 * 1000); | |
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(); | |
switch (status.getStatusCode()) { | |
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: | |
try { | |
// Show the dialog by calling startResolutionForResult(), | |
// and check the result in onActivityResult(). | |
status.startResolutionForResult(MapsActivity.this, REQUEST_LOCATION); | |
// finish(); | |
} catch (IntentSender.SendIntentException e) { | |
// Ignore the error. | |
} | |
break; | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment