【问题标题】:Make a call using action_call intent on Android 10 doesn't work在 Android 10 上使用 action_call 意图拨打电话不起作用
【发布时间】:2020-08-19 22:16:48
【问题描述】:

这适用于以前版本的 android,但在 android 10 上它不再适用。任何想法如何解决这个问题。任何帮助将不胜感激 。我已尝试使用来自 telemanager 的意图 act​​ion_call 和 placeCall。

        /**
         * Call a given number
         *
         * @param context
         * @param number
         */
        public static void call(@NotNull Context context, @NotNull String number) {
            try {
                // Create call intent
                Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode(number)));
                // Handle sim card selection
    //            int simCard = getSimSelection(context);
    //            Timber.d("simcard "+simCard);
    //            if (simCard != -1) callIntent.putExtra("com.android.phone.extra.slot", simCard);
                
                callIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
                // Start the call
                context.startActivity(callIntent);
            } catch (SecurityException e) {
                Toast.makeText(context, "Couldn't make a call due to security reasons", Toast.LENGTH_LONG).show();
            } catch (NullPointerException e) {
                Toast.makeText(context, "Couldnt make a call, no phone number", Toast.LENGTH_LONG).show();
            }
        }


   /**
     * Places a new outgoing call to the provided address using the system telecom service with
     * the specified intent.
     *
     * @param activity       {@link Activity} used to start another activity for the given intent
     * @param telecomManager the {@link TelecomManager} used to place a call, if possible
     * @param intent         the intent for the call
     */
    public static boolean placeCall(@Nullable FragmentActivity activity,
                                    @Nullable TelecomManager telecomManager, @Nullable Intent intent) {
        if (activity == null || telecomManager == null || intent == null) {
            return false;
        }
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
        telecomManager.placeCall(intent.getData(), intent.getExtras());
        return true;
        //        activity.startActivityForResult(intent, 1291);
//        return true;
    }

【问题讨论】:

    标签: java android android-intent


    【解决方案1】:

    尝试添加 callIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);

    【讨论】:

    • 非常感谢。? 但我已经尝试过了。没用。
    【解决方案2】:

    您是否尝试从后台服务发起呼叫?

    Android 10 仅限于从后台启动活动。对此有一些例外情况。在我看来,请求“SYSTEM_ALERT_WINDOW”权限是最简单的。

    https://developer.android.com/guide/components/activities/background-starts

    https://stackoverflow.com/a/59421118/11982611

    private void RequestPermission() {
            // Check if Android M or higher
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // Show alert dialog to the user saying a separate permission is needed
                // Launch the settings activity if the user prefers
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getActivity().getPackageName()));
                startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            }
        }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(getContext())) {
                    PermissionDenied();
                }
                else
                {
                 //Permission Granted-System will work
            }
    
        }
    }
    

    【讨论】:

    • 我不是从后台调用它。出于某种原因,当我使用 action_call 或 placecall 时。我的电话没有进入拨号模式。
    • “不进入拨号模式”是什么意思?拨号器屏幕是否打开,里面有号码,但它只是不拨号?
    • 是的,它以号码和用户名打开。但没有拨号音。它只发生在 android 10 设备上。
    • 这是一项安全功能,因此应用程序不会自动拨打客户需要支付额外费用的号码。这样,即使应用程序有权启动拨号器,他仍然可以改变主意。您无法自动拨打号码 - 只能打开拨号器。
    • 但我的应用是默认拨号器。如果它是默认拨号器,有没有办法绕过它
    【解决方案3】:

    如果在设置调用之前未选择帐户,则需要您额外发送一个帐户句柄。

      try {
                List<PhoneAccountHandle> phoneAccountHandleList = TelecomUtil.getTelecomManager(context).getCallCapablePhoneAccounts();
                int simCard = getSimSelection(context);
    
                // Create call intent
                Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode(number)));
    
                if (phoneAccountHandleList != null && !phoneAccountHandleList.isEmpty()) {
                    callIntent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandleList.get(simCard));
                }
    //            // Handle sim card selection
                Timber.d("simcard %s", simCard);
                if (simCard != -1) callIntent.putExtra("com.android.phone.extra.slot", simCard);
    //            // Start the call
                context.startActivity(callIntent);
    
            } catch (SecurityException e) {
                Toast.makeText(context, "Couldn't make a call due to security reasons", Toast.LENGTH_LONG).show();
            } catch (NullPointerException e) {
                Toast.makeText(context, "Couldnt make a call, no phone number", Toast.LENGTH_LONG).show();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多