Android Permission Request Code Issue
10,866
Solution 1
For lower versions you need to declare permission in manifest only, but for marshmellow you need to give it in the code, where you want to execute the code.
Here, you want to make a call. So, insert/include the code provided below in the code block written to make the call.
public void makeCall(){
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "123456"));
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
if (result == PackageManager.PERMISSION_GRANTED){
startActivity(intent);
} else {
requestForCallPermission();
}
}
private void requestPermission(){
if(ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CALL_PHONE)){}
else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CALL_PHONE},PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makeCall();
}
break;
}
}
Solution 2
public void makeCall(String s)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + s));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
requestForCallPermission();
} else {
startActivity(intent);
}
}
public void requestForCallPermission()
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CALL_PHONE))
{
}
else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makeCall("100");
}
break;
}
}
//Now call the method makeCall("your_desire_phone_numder"); makeCall("100"); Link for more details
Author by
ueen
Updated on June 28, 2022Comments
-
ueen almost 2 years
How to request a Permission? I tried to documentation, but the constant int request code MY_PERMISSIONS_REQUEST_CALL_PHONE donst seem to just work, anything else to bear in mind for Backwards compatibility?
ActivityCompat.requestPermissions(getApplicationContext(), new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
How to declare MY_PERMISSIONS_REQUEST_CALL_PHONE constant int?
-
ueen about 8 yearsThanks it started working i guess i put the startActivity in the if before :)
-
Sharad Chauhan about 8 yearsYeah that was the only problem. Keep Learning. :)
-
ueen about 8 yearsI need the Permisson on startup and make the call on button click. And i NEED the permisson so denying is no option...
-
ueen about 8 yearsThis requiers API23 my app is minSDK 15
-
Drup Desai about 8 yearsyes you required compile SDK version to 23. Min 15 would work.
-
ueen about 8 yearsYea, i understand that, but how to delcare PERMISSION_REQUEST_CODE
-
Vickyexpert about 8 yearsSorry, I forgot to inform you about it, you need to declare it at top like below private static final int PERMISSION_REQUEST_CODE = 1;
-
Vickyexpert about 8 yearsAnd also change permission in requestPermission() method at else part by CALL_PHONE instead of ACCESS_FINE_LOCATION