Make USSD call in android

AndroGeek picture AndroGeek · Jun 26, 2013 · Viewed 31.8k times · Source

To check the balance first i have to make a call *xxx# and then i get a response with the multiple options to choose from and after i input the particular number i get the balance.

What code can i use for the same in my android app?

Dialing *xxx*x# is giving me error.

Below is my code which works fine for the *xxx# calls:

String encodedHash = Uri.encode("#");
String ussd = "*" + encodedHash + lCallNum + encodedHash;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ussd)));

Answer

Luís Silva picture Luís Silva · Feb 26, 2014

This works for me:

private Uri ussdToCallableUri(String ussd) {

    String uriString = "";

    if(!ussd.startsWith("tel:"))
        uriString += "tel:";

    for(char c : ussd.toCharArray()) {

        if(c == '#')
            uriString += Uri.encode("#");
        else
            uriString += c;
    }

    return Uri.parse(uriString);
}

Then in work code:

Intent callIntent = new Intent(Intent.ACTION_CALL, ussdToCallableUri(yourUSSDCodeHere));
startActivity(callIntent);