How is it possible to do USSD requests on Android?

2019-01-06 15:21发布

Some custom dialer apps (for example, Dialer from MotoBlur) are able to do USSD requests. Is it realy impossible to do this via SDK?

标签: android ussd
5条回答
女痞
2楼-- · 2019-01-06 15:36

Ussd api was added in API26. So since Oreo working with ussd looks smt like this:

    TelephonyManager manager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
    manager.sendUssdRequest("*100#", new TelephonyManager.UssdResponseCallback() {
        @Override
        public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
            super.onReceiveUssdResponse(telephonyManager, request, response);
        }

        @Override
        public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
            super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);
        }
    }, new Handler());

    TelephonyManager manager2 = manager.createForSubscriptionId(subIdForSecondSlotFromSubscriptonManager);
    manager2.sendUssdRequest(...);
查看更多
聊天终结者
3楼-- · 2019-01-06 15:37

hope it work for you:

String suffix = Uri.encode("#");
String ussd = suffix+"919"+"*"+number+suffix;

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ ussd));
startActivity(callIntent);
查看更多
我命由我不由天
4楼-- · 2019-01-06 15:38

You can dial ussd requests like any other number with an call-intent like this one:

String encodedHash = Uri.encode("#");
String ussd = "*" + encodedHash + "12345" + encodedHash;
startActivityForResult(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussd)), 1);

However, afaik, it's currently not possible to parse the result string in your app.

查看更多
Bombasti
5楼-- · 2019-01-06 15:41

Starting from Android O it's possible to send USSD requests using the TelephonyManager.

查看更多
Rolldiameter
6楼-- · 2019-01-06 15:43

You can intercept the USSD reponse , In order to do that you need to implement IExtendedNetworkService.aidl interface which binds the service with PhoneUtils. It then can intercept any USSD response and you can read that in your app easily . FYI https://github.com/alaasalman/ussdinterceptor

查看更多
登录 后发表回答