Reputation:
I have an application which ask for permission from the user to start the device's bluetooth. I want to know whether if the user accepted or denied the permission. Is there any method to do that.
Upvotes: 0
Views: 945
Reputation: 5486
You must override onRequestPermissionsResult method. For example, here you check if user granted permision for access to his location:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
//Do something here
}
}
}
}
Also, before that when you ask your permission you must determine your requestCode, which will be also sent to this method, when the user will be asked for permission. So it would be something like here:
if (ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
So now, my requestCode is equal to 1(and i am checking that later in my onActivityRestult method.
Hope this helps.
Upvotes: 2