Intercepting USSD calls in Android

Dialing a USSD code from a custom activity is straight forward using a DIAL or CALL intent, but listening to the returned result is not due to Android not having proper support for intercepting USSD calls within the platform, but partial though undocumented support exists within the native dialer application.

As a start, look at the PhoneUtils class in the Android source code. The link is for 4.0.3 but I believe this partial support has been present since 2.3.

Specifically, looking at line 217, an intent with the name “com.android.ussd.IExtendedNetworkService” is being composed. So what you need to do is implement your own service that responds to that intent. The service needs to be implemented according to the IExtendedNetworkService.aidl which is a part of the Android framework.

The aidl exposes several functions but the one we care about is the getUserMessage(text) function in that service. The text is the final value returned from the USSD call.

Notes:

  • Since the service is binded on by PhoneUtils, then you need to start the service at phone boot up. It also means that any modification to the service will need a phone reboot.
  • Returning null from getUserMessage will suppress the dialer from showing the USSD result but there’s no way to completely hide the dialer.
  • You can also use the other functions to change the text shown while the call is in progress
  • This doesn’t seem to work on USSD prompts(menus), only on final results

Nothing explains code like a code sample, right? So I wrote a sample service and you can take a look at this link.

Inspiration for this post and code came from this Russian blog post.

17 thoughts on “Intercepting USSD calls in Android

  1. Pingback: Blogged about intercepting USSD calls in Android o… | Code Vault

  2. Shruthi

    Thanks for this detailed information.

    I have been working on a similar requirement now and appreciate if you can help me on these 2 aspects:
    1. The default system dialog appears even when we make a USSD call from our activity. Can this dialog be hidden?. Returning null from getUserMessage does not hide the dialog.

    2. And is there a work around to USSD call that returns a menu as response?

  3. Amit

    Hi,I tried installing this app,but nothing happens.There’s a warning message saying “No Launcher activity found!The launch will only sync the application package on the device!” appears.I am new to this.Please tell me whats wrong?

  4. Jimmy Ilenloa

    How do we intercept USSD on Android 4.3 and 4.2 using the code you provide. I succeeded in making use of it on Android 2.3 to 4.0

    1. omasampath

      Hi,n

      I’m doing USSD base project for my university. Pls give a sample to use this interface.

      thanks,

      Sampath

  5. Maria

    there any way to run the program to start an application and not force you to restart the terminal?

  6. Dane

    hi! do you have a working application about this one? we are trying to develop an android application that will receive and dial on ussd codes. Thanks a lot!

Comments are closed.