3

I try to use TelephonyManager.UssdResponseCallback and catch USSD request. My code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 0);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                Handler handler = new Handler();

                TelephonyManager.UssdResponseCallback responseCallback = new TelephonyManager.UssdResponseCallback() {
                    @Override
                    public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
                        super.onReceiveUssdResponse(telephonyManager, request, response);

                        Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
                        super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);

                        Toast.makeText(MainActivity.this, String.valueOf(failureCode), Toast.LENGTH_SHORT).show();
                    }
                };

                telephonyManager.sendUssdRequest("*123#", responseCallback, handler);
            }
        });
    }

But every time I got "USSD_RETURN_FAILURE". Is there any way to test USSD on emulator? Or thre is bug in my code?

Hagakurje
  • 1,002
  • 2
  • 11
  • 17
  • Please tell me you found a solution for this... I'm facing the same issue not on emulator (did not tried it there) but on my smartphone...Android oreo 8.1 – Jason Krs May 27 '18 at 13:31
  • There is no solution for now. I opened issue https://issuetracker.google.com/issues/69251738 And I have bought a new phone with Android 8... – Hagakurje May 28 '18 at 06:18
  • 2
    Take a look at [this](https://ibb.co/dMLZJJ) image and at the 10th reply of [this](https://androidforums.com/threads/how-to-use-api-27-telephony-manager-ussd-feature.1271192/) discussion. I have found that even though we get `USSD_RETURN_FAILURE`, the request actually works but the result from the network is not in `onReceiveUssdResponse` but is actually in an overriden method of `sendUssdRequest` called `onReceiveResult`.... If you find a way to circumvent this, let me know... The problem may be somewhere about the `sendUssdRequest` not able to parse the ussd string supplied to it – Jason Krs May 28 '18 at 06:41
  • 9
    Problem solved here. I believe the USSD code you provided is a opening a multi-session USSD. For instance, choose 1 for balance or 2 connection data .... I changed my USSD request to a code that does not trigger a multi-session USSD. Then it all worked as expected... The `sendUssdRequest` response handler does not know how to deal with multisessions USSD request so it just goes for `USSD_FAILURE_REQUEST` – Jason Krs May 28 '18 at 10:14
  • 1
    its work fine with single session ussd, only problem with multi session USSD like press 1 for balance , press 2 for internet data etc – vikas singh Apr 03 '19 at 07:01
  • So what did you do? Do you create and terminate sessions? – TheRealChx101 Apr 22 '19 at 18:26
  • It does not work on emulator. On real device it works. – Hagakurje Apr 23 '19 at 05:11
  • @vikassingh looking forward the android API that handle multi-step USSD session. in the meantime i use this accessibility-based solution https://stackoverflow.com/questions/35793378/android-interacting-with-ussd-dialog-programmatically/44480576#44480576 – Abdu Jul 01 '19 at 10:40
  • For anyone searching for a way, I found platform that allows an app to handle multi-step USSD sessions called AutoUssd. You can checkout their site at [AutoUssd https://autoussd.com](https://autoussd.com) – Kwame Opare Asiedu Jun 26 '21 at 22:02

0 Answers0