в моем приложении я запрашиваю разрешение на местоположение у пользователя после предоставления разрешения. Я хочу проверить, включено ли местоположение, если оно не активно, тогда я хочу, чтобы пользователь перешел к действию настроек, где они могут включить местоположение.Вот код, который я пытаюсь выполнить вышеупомянутым условием, но после предоставления разрешения, когда местоположение не включено, оно не переходит к действию настроек.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
//This will run when permission is already granted.
else {
if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
else {
mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLocation != null) {
String str1 = hereLocation(mLocation.getLatitude(), mLocation.getLongitude());
fragText.setText(str1);
sendLocation(str1);
}
}
}
}
else{
//For Api level below 23
if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
else {
mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLocation != null) {
String str1 = hereLocation(mLocation.getLatitude(), mLocation.getLongitude());
fragText.setText(str1);
sendLocation(str1);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED){
if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
else {
mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLocation != null) {
String str1 = hereLocation(mLocation.getLatitude(), mLocation.getLongitude());
fragText.setText(str1);
sendLocation(str1);
}
}
}
else{
progressBar.setVisibility(View.INVISIBLE);
TastyToast.makeText(getActivity(),"Unable to get location",TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
}
}
}
Пожалуйста, дайте мне знать проблему в приведенном выше коде.
СПАСИБО