4

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



        }
    });
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
  • See [this answer](https://stackoverflow.com/a/35547547/4768284). – Kunal Chawla Jun 07 '17 at 06:43
  • USSD codes are not supported in Android. There is no reliable way to do it that will work on all devices. Nor should you really be using them anyway- USSD were a hack that existed before data plans. These days you should call a webservice instead. – Gabe Sechan Jun 07 '17 at 06:48
  • Is there a way to put a '#' . Why the whole string cannot be taken here – Haritha Gayashan Jun 07 '17 at 08:11

4 Answers4

4

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

Example:

Uri.parse("tel:"+ Uri.encode("*100#"));
Faraz
  • 2,144
  • 1
  • 18
  • 28
2

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);
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0

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.

rejdrouin
  • 69
  • 1
  • 4
0

The short answer for the question is :

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

See my detailed answer for more ;)

rejdrouin
  • 69
  • 1
  • 4