stm
stm

Reputation: 41

Android - Run USSD *#0011# using USSD API on a Samsung device

Recently my family and I moved to a new house and we have a little phone service problem. I have a spare device, and I started to develop Android apps.

I read about how you can check your signal strength (we all have Samsung devices), and I came across the USSD code *#0011#. After a day of research I have found the USSD API and started to write an application that will eventually display the cell's signal strength (to my phone).

When I send the USSD code, the callback always fails, the onReceiveUssdResponseFailed() is called. What can I try to resolve this?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 0);

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Handler handler = new Handler();

        TelephonyManager.UssdResponseCallback responseCallback = new TelephonyManager.UssdResponseCallback() {
            @Override
            public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
                super.onReceiveUssdResponse(telephonyManager, request, response);

                Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
                super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);

                Toast.makeText(MainActivity.this, "Failed " + request, Toast.LENGTH_LONG).show();

            }
        };

        try {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            telephonyManager.sendUssdRequest("*#0011#", responseCallback, handler);
        } catch (NullPointerException e){
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 2329

Answers (2)

Kwame Opare Asiedu
Kwame Opare Asiedu

Reputation: 2345

There's an awesome service called AutoUssd (https://autoussd.com) that allows you to do just this. (I.e. dial a USSD code from within your app)

The SDK allows you to create interactive, multi-step USSD sessions which are hidden behind a layout file and the results of a session is reported to your app via a callback.

Kindly check it out to see if it fits your needs...

Upvotes: 0

ahmed350331
ahmed350331

Reputation: 1

evelop android apps. I read about how you can check your signal strength (we all have Samsung devices), and i came across the USSD code *#0011#. After a day of research I have found the USSD API and started to write an application that will eventually display the cell's signal strength (to my phone). When I send the USSD code, the callback always fails, the onReceiveUssdResponseFailed() is called. Any help?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 0);

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Handler handler = new Handler();

        TelephonyManager.UssdResponseCallback responseCallback = new TelephonyManager.UssdResponseCallback() {
            @Override
            public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
                super.onReceiveUssdResponse(telephonyManager, request, response);

                Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
                super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);

                Toast.makeText(MainActivity.this, "Failed " + request, Toast.LENGTH_LONG).show();

            }
        };

        try {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            telephonyManager.sendUssdRequest("*#0011#", responseCallback, handler);
        } catch (NullPointerException e){
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Related Questions