Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to do USSD requests on Android?

Tags:

android

ussd

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

like image 992
artem Avatar asked Mar 29 '11 19:03

artem


3 Answers

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

    }
like image 123
Mikhail Sidorov Avatar answered Nov 18 '22 06:11

Mikhail Sidorov


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

like image 9
Mani Avatar answered Nov 18 '22 07:11

Mani


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.

like image 9
alexleutgoeb Avatar answered Nov 18 '22 08:11

alexleutgoeb