Haritha Gayashan
Haritha Gayashan

Reputation: 45

Android studio Action call to run USSD codes

By using an app I want to run a USSD code, but the problem is it doesn't recognize the "#" symbol. If I want to run "*100#" it recognize the input only as "*100". How to add the "#". and what is the reason to not recognize that?

Here is my code ...

  checkBalance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent i = new Intent(Intent.ACTION_CALL);


            i.setData(Uri.parse("tel:"+"*100#"));

            if (ActivityCompat.checkSelfPermission(mobitelPage.this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            startActivity(i);



        }
    });

Upvotes: 4

Views: 6609

Answers (4)

rejdrouin
rejdrouin

Reputation: 89

The short answer for the question is :

"#" must be changed to its "%23" ASCII value.

See my detailed answer for more ;)

Upvotes: 0

rejdrouin
rejdrouin

Reputation: 89

There is an important difference between MMI and USSD codes. USSD are executed as soon as the final "#" is entered while MMI codes require a click on the "Call" button.

IMPORTANT : In all cases, "#" must be changed to its "%23" ASCII value.

1- MMI codes work with the android.intent.action.DIAL action. Here is a Command line example for MMI code *#43# :

    adb shell am start -a android.intent.action.CALL -d tel:*%2343%23

Will trigger interaction between device and network to report Call waiting status and display the toast "Call Waiting / The service is activated" (or .. is not ..).

In your code; something like this will work :

    DeviceManipulations.call(phoneNumber.replace("#", "%23"));

2- USSD fail when used with CALL action. Use android.intent.action.DIAL. Plus, it takes a few tricks :

With command line, you must put the code between double quotes. Example for MMI code *#06# :

    adb shell am start -a android.intent.action.DIAL -d tel:"*%2306%23"

This displays the Device Information popup.

With my DeviceManipulations code, using DIAL with "*%2306%23" leaves the dialer opened with the code but the execution does not start. Workaround is to do it in two parts : DIAL without the last "#" :

    int intlong = codeUSSD.length();
    codeUSSD = codeUSSD.substring(0, (intlong-1));
    DeviceManipulations.dial(codeUSSD.replace("#", "%23"));

.. followed with "input keyevent" for the last "#" :

    DeviceManipulations.customExec("adb shell input keyevent 18");

I suppose that sending every single caracter with "intput keyevent" method would work best but the above is a shortcut.

Note : The "input keyevent 18" does not trigger action with Samsung devices. Those have a different dialer than Google's. I have to use an Appium interaction with those.

Upvotes: 0

Faraz
Faraz

Reputation: 2154

You need to use Uri.encode("YOUR USSD CODE") in Uri.Parse().

Example:

Uri.parse("tel:"+ Uri.encode("*100#"));

Upvotes: 4

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

Try this code.Use Uri.encode("#"):

    String encodedHash = Uri.encode("#");
    i.setData(Uri.parse("tel:"+"*100"+encodedHash));

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    startActivity(i);

Upvotes: 2

Related Questions