USSD on Android

USSD (Unstructured Supplementary Service Data) is a standard service in GSM networks, which allows organizing interactive interaction between a network subscriber and a service application in the mode of sending short messages.
As you know, Android does not have an API for reading USSD messages, then I will tell you how to solve this problem.

Somehow I was faced with the task of sending a command and receiving a USSD message. It turns out that the USSD response is stored in the BufferedReader buffer and there is a third-party USSD class for parsing information from this buffer.

The class itself can be copied here.

Now let’s look at the implementation of the class itself:
First, create the application interface, we will have AutoCompleteTextView, TextView, Button:


   android: orientation = "vertical" 
   android: layout_width = "fill_parent" 
   android: layout_height = "fill_parent">
       android: layout_width = "fill_parent" 
       android: text = "" 
       android: layout_height = "wrap_content" 
       android: inputtype = "phone | textUri"
       android: id = "@ + id / Text1">


       android: layout_width = "fill_parent" 
       android: id = "@ + id / Text2" 
       android: layout_height = "wrap_content">

       android: text = "@ string / send"
       android: id = "@ + id / button1" 
       android: layout_width = "fill_parent" 
       android: layout_height = "wrap_content">



This is the interface we should have:

image

Now we will bring the application to life, we will write the code for receiving the USSD result.

// I do not write all the imports, I only write that do not forget to connect the USSD class
import com.example.android.UssdMessage.USSD;
 
public class UssdmessageActivity extends Activity implements OnClickListener {
/ ** Called when the activity is first created. * /
private TextView view;
private AutoCompleteTextView number;
 
Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
Button button = (Button) findViewById (R.id.button1);
button.setOnClickListener (this);
this.view = (TextView) findViewById (R.id.Text2);
this.number = (AutoCompleteTextView) findViewById (R.id.Text1);
}
 
Override
public void onClick (View arg0) {
String encodedHash = Uri.encode ("#");
call ("*" + number.getText () + encodedHash);
this.view.setText ("");
}
 
protected void call (String phoneNumber) {
try {
startActivityForResult (
new Intent ("android.intent.action.CALL", Uri.parse ("tel:"
+ phoneNumber)), 1);
} catch (Exception eExcept) {
this.view.append ("nn" + "n" + eExcept.toString ());
}
}
 
Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
USSD ussd = new USSD (4000,4000); // two parameters are passed, the delay before and after (ms) the creation of the message
if (ussd.IsFound ())
this.view.append ("n" + ussd.getMsg ());
else
this.view.append ("" + R.string.error_ussd_msg);
}
 
}


Also, do not forget to add the following permissions (otherwise it will not work):



Screenshot of the application:

image

I’ll add that the class is written so that it displays debugging information in logcat.
In my opinion, it is not a complicated and convenient class that can help in solving your problems.
Thanks for attention.

.apk file Download
source Download

Also popular now: