Some custom dialer apps (for example, Dialer from MotoBlur) are able to do USSD requests. Is it realy impossible to do this via SDK?
Asked
Active
Viewed 2.6k times
5 Answers
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);
}
}

Oluleye IResþekt Idowu
- 367
- 1
- 5
- 19

Mikhail Sidorov
- 699
- 2
- 9
- 18
-
1Hey 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 yet – Jason Krs May 26 '18 at 21:37
-
manager2 code is for dual sim phones, when you what to send ussd from the second simcard. subIdForSecondSlotFromSubscriptonManager - is a second slot ID – Mikhail Sidorov Jun 04 '18 at 15:23
-
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 phone – Jason Krs Jun 09 '18 at 17:25
-
1You can get a list of subscriptions and find in it subscription for slot `List
sims = SubscriptionManager.from(context).getActiveSubscriptionInfoList(); for(SubscriptionInfo subInfo : sims){ ... slotIndex = subInfo.getSimSlotIndex() ... subscriptionForSlot = subInfo.getSubscriptionId() }` – Mikhail Sidorov Jun 10 '18 at 18:02
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

Mani
- 176
- 3
- 11
-
4It wont work with 4.2.2 due to security restrictions(changes have been made to PhoneApp code) – Johnny Doe Dec 02 '13 at 06:53
-
please can any one help me to get the response and redirect to other activity – khouloud mejdoub Apr 09 '14 at 18:24
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.

alexleutgoeb
- 3,123
- 3
- 20
- 31
-
3You 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. – Chris Stratton Aug 29 '11 at 01:04
-
2
-
1This is just part of an actual USSD number and therefore an implementation detail of a local operator, nevermind – alexleutgoeb Mar 05 '15 at 16:14
-
1Better way to construct Uri object: `Uri.fromParts("tel", "*#12345#", null)`. You don't need to encode/parse. – spatar Sep 19 '19 at 17:07
6
Starting from Android O it's possible to send USSD requests using the TelephonyManager
.

greywolf82
- 21,813
- 18
- 54
- 108
-
1
-
@davkutalek I know, it sucks. I wonder when will Google add that in – TheRealChx101 Feb 18 '22 at 07:29
-
2@TheRealChx101 we make an SDK that gets around this limitation, see usehover.com – davkutalek Feb 18 '22 at 17:09
-
@davkutalek How does it work now that Google blocks the accessibility + scraping method? – TheRealChx101 Feb 18 '22 at 19:44
-
-
1
-
@TheRealChx101 I had an answer here that explained it, but a moderator deleted it for spam – davkutalek Feb 21 '22 at 14:53
-
1@TheRealChx101 Hover seems to use accessibility service to click through the system dialog, as I understood it from their blog. https://medium.com/use-hover/in-context-permissions-with-the-hover-sdk-part-i-234be09b2b4c – Pavel Haluza Sep 10 '22 at 11:45
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);

Pecs Correia
- 71
- 4