How to Get Response from USSD code from Android?
Asked Answered
P

1

15

I have written an application that uses ussd code. I want to send a request for a ussd but I don't know how to get the data and save it in a String.

sample code:

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

    Button btn = (Button) findViewById(R.id.button1);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String encodedHash = Uri.encode("#");
            String ussd = "*141*1" + encodedHash;
            startActivityForResult(new Intent("android.intent.action.CALL",
                    Uri.parse("tel:" + ussd)), 1);

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    Toast.makeText(getApplicationContext(),
            "USSD: " + requestCode + "  " + resultCode + " ", 1).show();

    if (requestCode == 1) {

        if (resultCode == RESULT_OK) {
            // String result=data.getStringExtra("result");
            String dd = data.toString();
            Toast.makeText(getApplicationContext(), dd, 1).show();
        }

    }

Screenshots application: enter image description here

enter image description here

how to Resolve my Problem?

Pretoria answered 28/9, 2013 at 11:57 Comment(4)
Have you found the way to resolve your problem?Reductive
code.google.com/p/android/issues/detail?id=57120#c14Reductive
@sr.farzad: How did you solved your issue? Please provide me a solution as I'm looking for it!Bronnie
No,I couldn't Resolve My Problem.Beacause in android ,receive message from ussd not supported. and we can't Manage Respinse from UUSDPretoria
M
2

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.

Checkout an example code on github here.

Mada answered 5/5, 2015 at 16:27 Comment(4)
Can you post some relevant code that answers this question? It's a good idea to have the final answer be on the site, alongside the source references, since that makes SO an "end-all" for programming questions.Codeine
I already added link to a github repository that has the relevant code needed, its quite. It will be better you check out the repository. click here to see code.Mada
@Mada can you make a working example project that use your USSD Interceptor? I have a problem that cannot import the aidl file to my java classResuscitator
@Mada I implemented the code in the link you provided... My phone is Jelly Bean 4.1.1 ... After deploying the software, I powered off the phone and rebooted it... But when I trigger a USSD request, I don't see anything in Logcat....what am I doing wrong here ?Grilse

© 2022 - 2024 — McMap. All rights reserved.