Android Studio Make Ussd Request with a specific sim (Sim 1 or 2)

0
485

if you have been looking for how to make ussd calls with a specific sim number, then this is how to do it. To make ussd calls, we normally rely on using the phone dialer. Some phones usually present the user with the option of selecting either sim 1 or sim 2. This is how we can avoid that.

 
private static void dialCode(String ussdCodeTodial, Activity activity, int simNumber) {

        //check if it begins with * and ends with #
        String finalUssdCode = "";
        if (ussdCodeTodial.startsWith("*") && ussdCodeTodial.endsWith("#")) {
            ussdCodeTodial = ussdCodeTodial.substring(0, ussdCodeTodial.length() - 1);
            finalUssdCode = ussdCodeTodial + Uri.encode("#");
        } else if (ussdCodeTodial.startsWith("#") && ussdCodeTodial.endsWith("#")) {
            ussdCodeTodial = ussdCodeTodial.substring(1, ussdCodeTodial.length() - 1);
            finalUssdCode = Uri.encode("#") + ussdCodeTodial + Uri.encode("#");
        } else {
            finalUssdCode = ussdCodeTodial;
        }
        Log.e("TAG", "dialCode: dinal Ussd Code = " + finalUssdCode);


        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }


        Intent intent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse("tel:" + finalUssdCode));
        intent.putExtra("com.android.phone.force.slot", true);
        intent.putExtra("Cdma_Supp", true);
        for (String s : simSlotName) {
            intent.putExtra(s, simNumber); //0 or 1 according to sim.......
        }
        //0 for sim1

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            TelecomManager telecomManager = (TelecomManager) activity.getSystemService(Context.TELECOM_SERVICE);
            List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
            if (simNumber == 0) {
                if (phoneAccountHandleList != null && phoneAccountHandleList.size() > 0)
                    intent.putExtra("android.telecom.extra.PHONE_ACCOUNT_HANDLE", phoneAccountHandleList.get(0));
            } else {

                if (phoneAccountHandleList != null && phoneAccountHandleList.size() > 1)
                    intent.putExtra("android.telecom.extra.PHONE_ACCOUNT_HANDLE", phoneAccountHandleList.get(1));

            }
        }


        activity.startActivity(intent);


    }

LEAVE A REPLY

Please enter your comment!
Please enter your name here