Monday, 20 February 2017

Permissions to make a call in Android Marshmallow (Api 23) and above.

public class CommonUtils {

private static final int REQ_CODE = 100;

public void checkCallPermission(String number){

//use ACTION_CALL for callsIntent callIntent = new Intent(Intent.ACTION_CALL);
//here number is desired phone number you wish to call.callIntent.setData(Uri.parse("tel:"+number));

 //check the permissions, if the device is running Android 6.0 and the applicaiton targetSdkVersion is 23 or above, asks the user to grant permission.

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
 //request permission from user, if the app didn't get the required permission ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE}, REQ_CODE );
 return;
 }else {
// if device has permissions
try{
//start activity and make phone call
startActivity(callIntent);
}catch (android.content.ActivityNotFoundException ex){ Toast.makeText(getActivity(),""+ex.getMessage(),Toast.LENGTH_SHORT).show();
}
}
}
}

No comments:

Post a Comment