I have a conflict that causes my device to hang when I put both read gps and phone number permissions in a single activity, but when I test the permissions in two different activities everything works fine.
Here is part of the permissions in the 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" />
Here the 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() {
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;
}
}
}
Can you tell me what is my mistake?
The problem is one
ArrayIndexOutOfBoundsException
and it is caused at this point, when checking the array that is obtained from the permissions, if the array does not have any permissions, you cannot get any element:I suggest you perform the validation to check first if grantResults actually has any permissions: