How is it possible to do USSD requests on Android?
Asked Answered
K

5

8

Some custom dialer apps (for example, Dialer from MotoBlur) are able to do USSD requests. Is it realy impossible to do this via SDK?

Katlaps answered 29/3, 2011 at 19:5 Comment(0)
C
10

You can intercept the USSD reponse , In order to do that you need to implement IExtendedNetworkService.aidl interface which binds the service with PhoneUtils. It then can intercept any USSD response and you can read that in your app easily . FYI https://github.com/alaasalman/ussdinterceptor

Chelsae answered 22/1, 2013 at 5:59 Comment(2)
It wont work with 4.2.2 due to security restrictions(changes have been made to PhoneApp code)Wrasse
please can any one help me to get the response and redirect to other activitySinghalese
G
16

Ussd api was added in API26. So since Oreo working with ussd looks smt like this:

    TelephonyManager manager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
    manager.sendUssdRequest("*100#", new TelephonyManager.UssdResponseCallback() {
        @Override
        public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
            super.onReceiveUssdResponse(telephonyManager, request, response);
        }

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

    TelephonyManager manager2 = manager.createForSubscriptionId(subIdForSecondSlotFromSubscriptonManager);
    manager2.sendUssdRequest(...);

To get the simIDs, you can use the below:

    SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

    List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();

    for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
        int subscriptionId = subscriptionInfo.getSubscriptionId();
        Log.d("Sims", "subscriptionId:" + subscriptionId);
    }

    if (subscriptionInfoList != null) {
        if (subscriptionInfoList.size() == 1) {
            sim1 = subscriptionInfoList.get(0).getDisplayName().toString();
            tvSim1.setText(sim1);
        } else {
            sim1 = subscriptionInfoList.get(0).getDisplayName().toString();
            sim2 = subscriptionInfoList.get(1).getDisplayName().toString();

            tvSim1.setText(sim1);
            tvSim2.setText(sim2);
        }

    }
Gates answered 23/10, 2017 at 13:34 Comment(4)
Hey Mikhail, you're the only one with the new API 27 code... COuld you elaborate a little bit ? I don't understand manager2 code for createForSubscriptionId.... So for now my USSD is not sent yetEllissa
manager2 code is for dual sim phones, when you what to send ussd from the second simcard. subIdForSecondSlotFromSubscriptonManager - is a second slot IDGates
Awesome.... I was looking for that... But how do I set the value for subIdForSecondSlotFromSubscriptonManager ? Also, is there a subIdForFirstSlotFromSubscriptonManager value for the first slot ? I'm saying that in case slot 2 is the primary slot in my dual sim phoneEllissa
You can get a list of subscriptions and find in it subscription for slot List<SubscriptionInfo> sims = SubscriptionManager.from(context).getActiveSubscriptionInfoList(); for(SubscriptionInfo subInfo : sims){ ... slotIndex = subInfo.getSimSlotIndex() ... subscriptionForSlot = subInfo.getSubscriptionId() }Gates
B
10

You can dial ussd requests like any other number with an call-intent like this one:

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

However, afaik, it's currently not possible to parse the result string in your app.

Banzai answered 1/4, 2011 at 9:19 Comment(4)
You can send requests, but there is no means for an application to obtain a result. Some developers have requested that this be added to android, but there's been no indication that the feature request has been accepted.Husha
But what is the reason behind appending # after *?Divergent
This is just part of an actual USSD number and therefore an implementation detail of a local operator, nevermindBanzai
Better way to construct Uri object: Uri.fromParts("tel", "*#12345#", null). You don't need to encode/parse.Constructivism
C
10

You can intercept the USSD reponse , In order to do that you need to implement IExtendedNetworkService.aidl interface which binds the service with PhoneUtils. It then can intercept any USSD response and you can read that in your app easily . FYI https://github.com/alaasalman/ussdinterceptor

Chelsae answered 22/1, 2013 at 5:59 Comment(2)
It wont work with 4.2.2 due to security restrictions(changes have been made to PhoneApp code)Wrasse
please can any one help me to get the response and redirect to other activitySinghalese
S
6

Starting from Android O it's possible to send USSD requests using the TelephonyManager.

Spectrohelioscope answered 13/6, 2017 at 6:27 Comment(8)
It still does not work for multi-step USSD sessionsEchopraxia
@Echopraxia I know, it sucks. I wonder when will Google add that inYaroslavl
@Yaroslavl we make an SDK that gets around this limitation, see usehover.comEchopraxia
@Echopraxia How does it work now that Google blocks the accessibility + scraping method?Yaroslavl
@Yaroslavl They don't, what makes you think they do?Echopraxia
So exactly how does hover work? Runs off backend GSM gateways?Yaroslavl
@Yaroslavl I had an answer here that explained it, but a moderator deleted it for spamEchopraxia
@Yaroslavl Hover seems to use accessibility service to click through the system dialog, as I understood it from their blog. medium.com/use-hover/…Jeanmariejeanna
S
0

hope it work for you:

String suffix = Uri.encode("#");
String ussd = suffix+"919"+"*"+number+suffix;

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ ussd));
startActivity(callIntent);
Sauerbraten answered 8/7, 2013 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.