1
I am having some difficulty in being able to request the permissions in Runtime, only the permissions accepting dialog of 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 question of permissions when executing the onClickButton method is immediately presented the dialog to allow the location, but after allowing the app crasha and by the logs reports the following error regarding the lack of permission for SMS. However if I boot the app again, now already with the permission for the guaranteed location, and run again the onClickButon method is presented the dialog for SMS permission.
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)
Also by the log’s I can see that the message is generated successfully, and by the printing of Toast 'Sent to 93xxxxxxx' I can see that the method was executed but in reality SMS never reaches the destination.
Can someone help me set this up so I can get everything functional...
In the manifest it’s all right right right? You’re giving permission normally?!
– viana
Yes in the manifest is right... It is something related to these Runtime permissions required by sdk 23 or higher... I don’t know how to fix this situation and I don’t know why then the SMS doesn’t reach the destination although everything seems to go well with sending
– Nuno Santos
You tried to add Activitycompat this way: Activitycompat.requestPermissions(new S...)
– viana