Tengo un conflicto que ocasiona que mi dispositivo quede colgado cuando coloco ambos permisos para leer gps y nro de teléfono en una sola actividad, pero cuando pruebo los permisos en dos actividades diferentes todo funciona correctamente.
Aqui parte de los permisos en el manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Aqui el MainActivity:
public class MainActivity extends AppCompatActivity {
TextView tvMensaje;
String mPhoneNumber;
private static final long MIN_TIME = 10000; // 10 segundos Minimo tiempo para updates en Milisegundos
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvMensaje = findViewById(R.id.tvMensaje);
//**************************************************************************************************
//* Permission to Read PhoneNumber
//**************************************************************************************************
if (ActivityCompat.checkSelfPermission(this, READ_SMS) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, READ_PHONE_NUMBERS)
== PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this,READ_PHONE_STATE)
== PackageManager.PERMISSION_GRANTED) {
TelephonyManager tMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
return;
} else {
requestPermission();
}
//******************************************************************************************
//* Permission to Read GPS
//******************************************************************************************
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000);
} else {
iniciarLocalizacion();
}
}
private void iniciarLocalizacion() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Localizacion local = new Localizacion();
local.setMainActivity(this, tvMensaje, mPhoneNumber);
final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!gpsEnabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000);
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, 0, local);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, local);
tvMensaje.setText("Inicializando GPS");
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{READ_SMS, READ_PHONE_NUMBERS, READ_PHONE_STATE}, 100);
}
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 100:
TelephonyManager tMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return;
}
String mPhoneNumber = tMgr.getLine1Number();
Log.e( "DEBUG-->", "Nro: " + mPhoneNumber );
break;
case 1000:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
iniciarLocalizacion();
return;
}
break;
}
}
}
Pueden decirme cual es mi error?