1

This works on the previous version of android but on android 10 it no longer works . any ideas how to solve this problem. any help would be greatly appreciated . I have tried with intent action_call and placeCall from telecomManager.

        /**
         * 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;
    }
Salman Khan
  • 895
  • 1
  • 8
  • 18

3 Answers3

2

try with adding callIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);

DoubleD
  • 148
  • 10
1

Are you trying to start a call from a background service?

Android 10 restricted to starting activity from background. There are some exclusions for this. In my view asking for "SYSTEM_ALERT_WINDOW" permission is the easiest one.

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
        }

    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Tal Mantelmakher
  • 994
  • 1
  • 7
  • 23
  • I am not calling it from the background . for some reason when i use action_call or placecall . my call does not go into the dial mode . – Salman Khan Aug 16 '20 at 03:23
  • What do you mean by "not go into dial mode"? Does the dialer screen open up, with the number inside, but its just not dialing? – Tal Mantelmakher Aug 16 '20 at 07:03
  • yes it opens up with number and user name .but no dial tone. it only happens on android 10 devices. – Salman Khan Aug 16 '20 at 07:49
  • This is a security feature so applications will not auto dial numbers where the customer will pay extra charges for example. This way he can still change his mind even if the app had permission to start the dialer. You cant automatically dial a number - only open the dialer. – Tal Mantelmakher Aug 16 '20 at 08:04
  • But my app is a default dialer . is there a way to bypass it if it is a default dialer – Salman Khan Aug 16 '20 at 08:25
  • My bad - just tested it myself, and it is possible to call in Android 10. Did you request runtime permission for CALL_PHONE? Also, what context are you passing to this method? please post the code that calls this method – Tal Mantelmakher Aug 16 '20 at 08:40
  • yes i requested permission.even used telecomManager.placeCall(intent.getData(), intent.getExtras()); instead of of action_call. the codes works on the emulator but doesn't on real devices. – Salman Khan Aug 16 '20 at 08:45
  • What context are you passing to this method? please post the code that calls this method – Tal Mantelmakher Aug 16 '20 at 09:09
  • I was talking about the method call(). Where are you calling it? What context are you passing to it? – Tal Mantelmakher Aug 16 '20 at 09:41
  • i am calling it from a fragment. – Salman Khan Aug 16 '20 at 09:50
  • Are you using Activity context with getActivity()? – Tal Mantelmakher Aug 16 '20 at 11:34
0

If the account is not selected before placing a call in setting it requires you to send a account handle as an extra.

  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();
        }
Salman Khan
  • 895
  • 1
  • 8
  • 18