I'm having trouble getting the runtime permissions request, and only the permissions acceptance dialog for one of the two required permissions is shown when I click the button.
/*Button PANIC Button
@ Click this button to send a PANIC message to defined contact
@ Inside the message go a PANIC message, name of person and GPS location
*/
Button button_panic = (Button) findViewById(R.id.button_panic);
button_panic.setOnClickListener(new OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)
{
requestLocationPermission();
requestSMSPermission();
String Message = "GRANTED";
Log.i("Location permission", Message);
}
else
{
sendMsg();
}
}
});
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void getLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//requestLocationPermission();
return;
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
String longitudeString = String.valueOf(location.getLongitude());
String latitudeString = String.valueOf(location.getLatitude());
message = "PANIC - Preciso de Ajuda " + Nome + " " + Apelido + ". " + "Esta é a minha localização: Latitude: " + latitudeString + " Longitude: " + longitudeString;
Log.e("Log Message", message);
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void sendMsg() {
getLocation();
SmsManager smsManager = SmsManager.getDefault();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
requestSMSPermission();
}
if (location != null) {
smsManager.sendTextMessage(Telefone, null, message, null, null);
Toast.makeText(MainActivity.this, "Sent to " + Telefone, Toast.LENGTH_SHORT).show();
} else {
smsManager.sendTextMessage(Telefone, null, "test", null, null);
Toast.makeText(MainActivity.this, "Please open your location service", Toast.LENGTH_SHORT).show();
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void requestSMSPermission() {
requestPermissions(new String[]{Manifest.permission.SEND_SMS}, REQUEST_PERMISSION_SEND_SMS_CODE);
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void requestLocationPermission() {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_FINE_LOCATION_CODE);
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PERMISSION_COARSE_LOCATION_CODE);
}
The permissions issue when executing the onClickButton method immediately displays the dialog to allow localization, but after allowing the crash app and the logs reports the following error regarding the lack of permission for SMS. However if I re-boot the app, now with the permission for the guaranteed location, and re-run the onClickButon method the dialog for the SMS permission is displayed.
java.lang.SecurityException: Sending SMS message: uid 10229 does not have android.permission.SEND_SMS. at android.os.Parcel.readException (Parcel.java:1620) at android.os.Parcel.readException (Parcel.java:1573) at com.android.internal.telephony.ISms $ Stub $ Proxy.sendTextForSubscriber (ISms.java:768) at android.telephony.SmsManager.sendTextMessageInternal (SmsManager.java:333) at android.telephony.SmsManager.sendTextMessage (SmsManager.java:299)
In addition to the log's I can see that the message is generated successfully, and by the impression of Toast 'Sent to 93xxxxxxx' I can see that the method was executed but SMS never really reaches its destination.
Someone can help me straighten this out so that I can have everything functional ...