Make USSD call in android

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-09 05:07:05

问题


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)));

回答1:


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);



回答2:


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

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



回答3:


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




回答4:


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




回答5:


Try this, I did not test it, but should work.

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode("*3282#")));
startActivity(intent);



回答6:


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" />



回答7:


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);


来源:https://stackoverflow.com/questions/17317981/make-ussd-call-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!