I have the following code in my program:
public static void callPhoneNumber(Context context, String clientPhoneNum) {
if (isCallingSupported(context)) {
Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + clientPhoneNum));
context.startActivity(i);
} else {
final AlertDialog alertDialog =
new AlertDialog.Builder(context).setMessage(context.getString(R.string.error))
.setMessage(context.getString(R.string.no_call_functionality))
.setPositiveButton(context.getString(R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
alertDialog.show();
}
}
private static boolean isCallingSupported(Context context) {
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return (telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE);
}
I am wondering if isCallingSupported()
would be necessary at all? I don't remember exactly why I wrote it this way but now when I am reviewing I'm thinking the user may just call a number using his Skype or other VOIP apps. Should I do any other checking instead or is this intent safe without the isCallingSupported()
(what I mean by safe is, even if user has a tablet with no calling functionality and no other apps that can handle a call, the intent doesn't cause a crash)?
Intent Object - Action to make Phone Call Intent phoneIntent = new Intent(Intent. ACTION_CALL); You can use ACTION_DIAL action instead of ACTION_CALL, in that case you will have option to modify hardcoded phone number before making a call instead of making a direct call.
A common intent action is ACTION_VIEW, which you use when you have some information that an activity can show to the user, such as a photo to view in a gallery app, or an address to view in a map app. You can specify the action for an intent in the intent constructor, or with the setAction() method.
Implicit Intent with URI data Uri number = Uri. parse("tel:11061991"); Intent callIntent = new Intent(Intent. ACTION_DIAL, number);
From this question:
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
//Then there is application can handle your intent
}else{
//No Application can handle your intent
}
Start by checking if any app has registered to this intent. If they have, use it. If not, display your dialog.
You would end up just replacing your isCallingSupported function with the code above:
private static boolean isCallingSupported(Context context) {
boolean result = true;
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() <= 0) {
result = false;
}
return result;
}
I would use PackageManager
and resolveActivity()
to see if anything will respond to your configured ACTION_DIAL
Intent
. That would handle:
Devices that do not have native telephony and have no dialer, as resolveActivity()
should return null
Devices that do not have native telephony but do have a dialer (e.g., VOIP), as hopefully the authors of that dialer have ACTION_DIAL
support
Devices that do have native telephony, but the current user does not have access to the dialer (e.g., restricted profiles)
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