Я написал код, чтобы проверить, включен ли gps или нет, и если он не включен, пользователь получает запрос с просьбой включить gps. При нажатии «ОК», GPS включается автоматически. Ниже приведен код, который я написал для проверки подключения GPS.
private void locationPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 22);
} else {
gpssettings(this);
}
} else {
gpssettings(this);
}
}
private void gpssettings(Context applicationContext) {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
Task<LocationSettingsResponse> task =
LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
fireauth();
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try {
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(
SplashScreen.this,
99);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
} catch (ClassCastException e) {
// Ignore, should be an impossible error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 99:
switch (resultCode) {
case RESULT_OK:
fireauth();
break;
case RESULT_CANCELED:
Toast.makeText(this, "Requires gps connection", Toast.LENGTH_SHORT).show();
finish();
break;
}
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 22){
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED){
gpssettings(this);
} else {
Toast.makeText(this, "Location permissions required", Toast.LENGTH_SHORT).show();
finish();
}
}
}
Но последние 10 дней ведут себя странно. Даже если пользователь нажимает «ОК», я получаю result_cancelled в activityResult. Пожалуйста, проверьте код и сообщите, если что-то не так или это какая-то другая проблема.