18

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)));
AndroGeek
  • 1,280
  • 3
  • 13
  • 25
  • Possible duplicate of http://stackoverflow.com/questions/11157828/sending-ussd-code-android?rq=1 Check this Stack Overflow question out. – user1725145 Jun 27 '13 at 08:18
  • It is not the possible duplicate of the above mentioned question. I want to know what would be the format of the USSD code for the above mentioned scenario where on dial of *XXX# gives me list of options like 1.Prepaid 2.Internet and so on.. and on entering 1 i get another set of options to choose from like enter 1 for balance 2 for something and so on.. – AndroGeek Jun 27 '13 at 08:39
  • Sending a USSD code is not the same as dialling a number. Have you got an Android interface for USSD? In the answer to that question, it is clear that as of November 2012, there was not an adequate USSD interface. Has that situation now changed? – user1725145 Jun 27 '13 at 09:21
  • It seems that the USSD problem has still not been solved. – user1725145 Jun 27 '13 at 09:25
  • 1
    USSD messages are not standard, so the format of the messages themselves depends upon what was defined by an operator or USSD user. – user1725145 Jun 27 '13 at 09:27

6 Answers6

26

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);
Luís Silva
  • 476
  • 5
  • 8
10

Don't forget to add permission it will solve skype problem:P

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
smj
  • 416
  • 1
  • 4
  • 10
6
String ussd = "*XXX*X" + Uri.encode("#");
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ussd)));

this works perfect with me. just place the first bunch as it is then encode the # to make it have a complete *XXX*X#. this will definitely be of help

user5021871
  • 61
  • 1
  • 2
6

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

Khurram Shehzad
  • 1,010
  • 12
  • 16
2

You can use this code. It works for me:

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(Uri.parse("tel:" + "*947") + Uri.encode("#")));
startActivity(intent);
Nayan
  • 1,521
  • 2
  • 13
  • 27
3bdoelnaggar
  • 1,109
  • 8
  • 18
2

Use this code, it works

Intent callIntent = new Intent(Intent.ACTION_CALL);
String ussdCode = "*" + 2 + Uri.encode("#");
callIntent.setData(Uri.parse("tel:" +ussdCode));

if (ActivityCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
startActivity(callIntent);

Add this line in Manifest file too

<uses-permission android:name="android.permission.CALL_PHONE" />
Nayan
  • 1,521
  • 2
  • 13
  • 27