简体   繁体   English

在android中进行USSD调用

[英]Make USSD call in android

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. 要先检查余额,我必须拨打电话* xxx#然后我会得到一个响应,有多个选项可供选择,在输入特定号码后我得到余额。

What code can i use for the same in my android app? 在我的Android应用程序中,我可以使用哪些代码?

Dialing *xxx*x# is giving me error. 拨打* xxx * x#给我错误。

Below is my code which works fine for the *xxx# calls: 下面是我的代码,适用于* xxx#调用:

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

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

不要忘记添加权限,它将解决skype问题:P

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
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# . 只需放置第一束,然后对#进行编码,使其具有完整的*XXX*X# this will definitely be of help 这肯定会有所帮助

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 如果您的目标是Android Marshmallow(6.0)或更高版本,那么您需要在运行时请求Manifest.permission.CALL_PHONE权限

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

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

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 在Manifest文件中也添加此行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM