android - 使用 ACTION_DIAL Intent 时,应用程序是否应该检查设备是否具有调用功能?

标签 android android-intent

我的程序中有以下代码:

  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);
  }

我想知道 isCallingSupported() 是否有必要?我不记得我为什么这样写,但现在当我回顾时,我认为用户可能只是使用他的 Skype 或其他 VOIP 应用程序调用一个号码。如果没有 isCallingSupported(),我应该做任何其他检查还是这个 Intent 安全(我所说的安全是指,即使用户的平板电脑没有调用功能,也没有其他应用程序可以处理调用, Intent 不会导致崩溃)?

最佳答案

来自 this问题:

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
}

首先检查是否有任何应用已注册到此 Intent。如果有,请使用它。如果没有,请显示您的对话框。

您最终会用上面的代码替换您的 isCallingSupported 函数:

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;

关于android - 使用 ACTION_DIAL Intent 时,应用程序是否应该检查设备是否具有调用功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26997863/

相关文章:

java - 响应数据未显示

Android 移动应用程序到 sd

android - 无论如何要在不执行 StartActivity 的情况下附加到 Activity 堆栈?

java - Intent 无法从应用程序内获得

android - 我如何通过将鼠标悬停在Visual Studio Code之类的android studio中的小部件上来获取定义

java - 以编程方式执行 Google Assistant

java - 根据命令更改 NavigationView 的内容(如按下按钮)

android - 使用起始值开始新 Activity

android - 我如何在我的 ScrollView 中调整我的 ImageView 的大小,因为它在这个图像中完成?

android - 覆盖android默认相机/Activity