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:
how to Resolve my Problem?
Unstructured Supplementary Service Data (USSD)—sometimes known as "quick codes" or "feature codes"—is an extra-UI protocol, which allows people to access hidden features.
USSD is a technology platform through which information can be transmitted through a GSM network on a basic phone. This service will be available on all mobile phones with SMS facility. To use USSD mobile banking, users will have to simply dial *99# and use the interactive menu.
USSD code running on your Android phone means that your phone is entered into the process to provide you with the information you want.
This app can be found on your home screen, on the "All Apps" screen, or, on simpler cell phones, on the lock screen. Dial the USSD code. Some start with *, others #, and others *#. Dial #.
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:
Checkout an example code on github here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With