使用 action_call 意图拨打电话 Android 10 不起作用

Make a call using action_call intent on Android 10 doesn't work

这适用于以前版本的 android 但在 android 10 上它不再适用。任何想法如何解决这个问题。任何帮助将不胜感激 。我已经尝试过 intent action_call 和来自 telecomManager 的 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;
    }

尝试添加 callIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);

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

Android 10 仅限从后台开始 activity。这有一些例外情况。在我看来,请求“SYSTEM_ALERT_WINDOW”许可是最简单的。

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

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
        }

    }
}

如果在设置中拨打电话之前未选择帐户,则需要您额外发送一个帐户句柄。

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