Does the application have to check if the device has a call function when using ACTION_DIAL intents?

My program has the following code:

  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'm wondering if it's necessary at all isCallingSupported()

? I don't remember exactly why I wrote it this way, but now as I browse I think the user can just call the number using their Skype or other VOIP apps. Should I be doing any other checks instead, or is the intent safe without isCallingSupported()

(I mean, safe, even if the user has a tablet with no call functions and no other applications that can handle the call, the intent is not to fail)?

+3


source to share


2 answers


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 is registered for this intent. If they are, use it. If not, display your dialog.



In the end, you just replace the 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;

      

}

+7


source


I would use PackageManager

and resolveActivity()

to see if anything answers your configured ACTION_DIAL

Intent

. This will handle:



  • Devices that do not have native telephony and do not have dialer, since they resolveActivity()

    must returnnull

  • Devices that do not have native telephony, but have a dialer (e.g. VOIP), we hope the authors of this dialer have ACTION_DIAL

    support

  • Devices that have their own telephone connection, but the current user does not have access to the dialer (for example, restricted profiles)

+3


source







All Articles