I have created a simple android activity that acts as a dial. It has an edit text for the phone number and a call button Here is the code : (android 6.0 marshmallow)
public class Main2Activity extends AppCompatActivity {
EditText num;
Button call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
num = (EditText) findViewById(R.id.num);
call = (Button) findViewById(R.id.call);
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
// request permission if not granted
if (ActivityCompat.checkSelfPermission(Main2Activity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Main2Activity.this, new String[]{Manifest.permission.CALL_PHONE}, 123);
// i suppose that the user has granted the permission
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num.getText().toString()));
startActivity(in);
// if the permission is granted then ok
} else {
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num.getText().toString()));
startActivity(in);
}
}
// catch the exception if I try to make a call and the permission is not granted
catch (Exception e){
}
}
});
}
}
When I run my app, I have these issues
If I click on the call button and grant permission, the intent is not called until I click again
I don't know how to check if the permission was granted or not
checkSelfPermission(activity, Manifest. permission. X) checks if any permission is already granted, if you check out other answers they do the same, and rest of the code asks for the permissions not granted.
The method shouldShowRequestPermissionRationale() can be used to check whether the user selected the 'never asked again' option and denied the permission.
Starting in Android 11, whenever your app requests a permission related to location, microphone, or camera, the user-facing permissions dialog contains an option called Only this time. If the user selects this option in the dialog, your app is granted a temporary one-time permission.
Use onRequestPermissionResult, It handles the action if user press ALLOW and DENY, Just call the intent in the condition "if the user presses allow":
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 123: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//If user presses allow
Toast.makeText(Main2Activity.this, "Permission granted!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num.getText().toString()));
startActivity(in);
} else {
//If user presses deny
Toast.makeText(Main2Activity.this, "Permission denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
Hope this helps.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
is terrible logic for checking permission result, i don't know why Google implemented such a terrible code.
It's a mess especially when you check multiple permissions. Let say you ask for
CAMERA
, ACCESS_FINE_LOCATION
and ACCESS_NETWORK_STATE
.
You need to check for ACCESS_FINE_LOCATION but user only granted CAMERA at first run and you check for grantResults[1] but in second run ACCESS_FINE_LOCATION becomes the permission with index 0. I got so many problems with user not granting all permissions at once.
You should either use
int size = permissions.length;
boolean locationPermissionGranted = false;
for (int i = 0; i < size; i++) {
if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION)
&& grantResults[i] == PackageManager.PERMISSION_GRANTED) {
locationPermissionGranted = true;
}
}
Or simpler one
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
// Do something ...
}
in onPermissionRequestResult
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With