⬆️ ⬇️

USSD in Android

USSD (Unstructured Supplementary Service Data) is a standard service in GSM networks, allowing to organize 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 faced the task of sending a command and receiving a USSD message. It turns out 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 consider the implementation of the use of the class itself:

First, let's create an application interface; we will have AutoCompleteTextView, TextView, Button:

')

<! -? xml version = "1.0" encoding = "utf-8"? ->

<linearlayout xmlns: android = "schemas.android.com/apk/res/android"

android: orientation = "vertical"

android: layout_width = "fill_parent"

android: layout_height = "fill_parent" >

<autocompletetextview

android: layout_width = "fill_parent"

android: text = ""

android: layout_height = "wrap_content"

android: inputtype = "phone | textUri"

android: id = "@ + id / Text1" >

<requestfocus > </ requestfocus >

</ autocompletetextview >

<textview

android: layout_width = "fill_parent"

android: id = "@ + id / Text2"

android: layout_height = "wrap_content" >

</ textview >

<button

android: text = "@ string / send"

android: id = "@ + id / button1"

android: layout_width = "fill_parent"

android: layout_height = "wrap_content" >

</ button >

</ linearlayout >




This is the interface we should have:



image



Now we will give our life to the application, we will write the code for obtaining the USSD result.



// I do not write all the imports, I will only write what 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 transmitted, delay before and after (ms) of message creation

if ( ussd. IsFound ( ) )

this . view . append ( "n" + ussd. getMsg ( ) ) ;

else

this . view . append ( "" + R. string . error_ussd_msg ) ;

}



}




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



<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> <uses-permission android:name="android.permission.CALL_PRIVILEGED"></uses-permission> <uses-permission android:name="android.permission.READ_LOGS"></uses-permission> 




Screenshot of the application:



image



I will add that the class is written in such a way that it displays debug information in logcat.

In my opinion, not a difficult and convenient class that can help in solving your problems.

Thanks for attention.



.apk file Download

source Download

Source: https://habr.com/ru/post/130717/



All Articles