Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make USSD call in android

Tags:

android

ussd

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)));
like image 477
AndroGeek Avatar asked Jun 26 '13 10:06

AndroGeek


2 Answers

Important thing to remember :

If your are targeting Android Marshmallow (6.0) or higher then you need to request Manifest.permission.CALL_PHONE permission at runtime

like image 64
Khurram Shehzad Avatar answered Sep 17 '22 13:09

Khurram Shehzad


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);
like image 22
Luís Silva Avatar answered Sep 20 '22 13:09

Luís Silva