As of version 6.0 (API level 23), permissions considered "dangerous"(Dangerous Permissions) are validated during the execution of the application and not when installing. This does not invalidate that they do not have to be declared in the Androidmanifest.xml.
The applications with targetApi 23+ that require Dangerous Permissions , to run on Android devices 6.0 or higher, you will need to adjust to this new approach.
Whenever the application executes an operation that requires a permission of this type it has to check if it has it before executing it.
This check is done by calling the method ContextCompat.checkSelfPermission():
int permissionCheck = ContextCompat.checkSelfPermission(context,
Manifest.permission.RECEIVE_SMS);
If the application has the permission is returned PackageManager.PERMISSION_GRANTED
otherwise returns PackageManager.PERMISSION_DENIED
If the application does not have the permission it is necessary to request it using the method ActivityCompat.requestPermissions():
private final int PERMISSIONS_REQUEST_RECEIVE_SMS = 1;
ActivityCompat.requestPermissions(context,
new String[]{Manifest.permission.RECEIVE_SMS},
PERMISSIONS_REQUEST_RECEIVE_SMS);
An dialog to ask the user for permission.
The dialog is shown asynchronously, the method being onRequestPermissionsResult()
called when the user responds.
That method should be implemented to address the:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_RECEIVE_SMS: {
// Se o pedido foi cancelado o array está vazio.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permissão foi concedida
// pode fazer o que pretende
} else {
// permissão foi negada
}
return;
}
// outros 'case' se tiver outras permissões para verificar
}
}
For more detailed information see documentation.
+1 Great explanation!
– user28595
Thanks ramaral I was reading the documentation after I posted the question. On this link https://github.com/googlesamples/android-RuntimePermissions there are examples from google of how to do this. (leaving the link in case someone finds it useful)
– Caique Oliveira
Sir, the dialog is open automatically or you have to do some XML for that?
– user41630
It opens automatically. You don’t have to call me "Sir":)
– ramaral
Ah I don’t get kk anymore let go of my app :)
– user41630