Slå på GPS programmässigt i Android 4.4 eller högre (10 / 12 steg)
Steg 10:
Nu bara kopiera och klistra in följande kod utanför oncreate metoden...
privat GoogleApiClient googleApiClient;
Nu bara kopiera och klistra in följande kod i oncreate...
om (googleApiClient == null) {
googleApiClient = ny GoogleApiClient.Builder(getApplicationContext()).addApi(LocationServices.API) .build(); googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(30 * 1000); locationRequest.setFastestInterval(5 * 1000); LocationSettingsRequest.Builder builder = nya LocationSettingsRequest.Builder() .addLocationRequest(locationRequest);
// **************************
builder.setAlwaysShow(true); Detta är en viktig ingrediens
// **************************
PendingResult resultat = LocationServices.SettingsApi .checkLocationSettings (googleApiClient, builder.build()), result.setResultCallback (nya ResultCallback)
{
public void onResult (LocationSettingsResult resultat)
{
slutliga Status status = result.getStatus();
LocationSettingsStates sluttillstånd = resultatet .getLocationSettingsStates();
Switch (status.getStatusCode())
{
fall LocationSettingsStatusCodes.SUCCESS:
bryta;
fall LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
försök {
status.startResolutionForResult (MainActivity.this, 1000);
} fånga (IntentSender.SendIntentException e)
{
}
bryta;
fall LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
bryta;
}
});
}
googleApiClient = ny GoogleApiClient.Builder(this).addApi(AppIndex.API).build();