I am currently working with Telephony Manager(USSD response) available in android api level 26(Nexus 6P). For single step ussd session, it's working.
example:
USSD request : "A" (ussd session initiates)
USSD response : "X" (ussd session terminates)
TelephonyManager = telephonyManager(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Handler handler = new Handler();
TelephonyManager.UssdResponseCallback callback = new TelephonyManager.UssdResponseCallback() {
@Override
public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
super.onReceiveUssdResponse(telephonyManager, request, response);
Log.e("ussd",response.toString());
}
@Override
public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);
Log.e("ussd","failed with code " + Integer.toString(failureCode));
}
};
try {
Log.e("ussd","trying to send ussd request");
telephonyManager.sendUssdRequest("*123#",
callback,
handler);
}catch (Exception e){
String msg= e.getMessage();
Log.e("DEBUG",e.toString());
e.printStackTrace();
}
but for interactive ussd request-response(multi-step), it's not working. Multi step scenario is as follows:
step # 1.
USSD request : "A" (ussd session initiates)
USSD response : "X"
step # 2.
USSD request : "B" (ussd session continues)
USSD response : "Y"
step # 3.
USSD request : "C"
USSD response : "Z" (ussd session terminates)
}
if your using dual sim make sure change to the network that your going to test(In my i used Airtel(*121#).
These APIs don't properly handle menu-based USSDs. Their intended use case would be for things like querying simple things like minutes or data left in the user's plan. For menu-based systems there would need to be some notion of a continued session, which is not something the APIs support.
I managed to bypass some of the problems (like the multisession USSD responses not working) by using Reflection. I've made a GitHub gist here.
Obviously the assumption is that, the correct permissions (only
CALL_PHONE
at this point) are given - In terms of Contexts, I've only ever run this in an Activity but I think it would work fine in most if not all/any.The next thing is figuring out how to sustain the session, if at all possible.
I am able to get a menu, respond to it by sending a number but past that, I guess am at the mercy of the telco - they send a menu that blocks the entire screen and if you cancel it the entire session dies. Having worked in telecom, I think that this might be different from telco to telco because some of them have gateways that can kill a user initiated session and replace it with a telecom side originated session. technically, the 2 sessions are detached. but here is my code:
please ignore the call answering stuff - I was using this app before to test automatic call answering on Oreo. And below is the Layout file for the display: