6

一些自定义拨号器应用程序(例如,来自 MotoBlur 的拨号器)能够执行 USSD 请求。通过SDK真的不可能做到这一点吗?

4

5 回答 5

10

在 API26 中添加了 ussd api。因此,由于 Oreo 使用 ussd 看起来像这样:

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

要获取 simID,您可以使用以下命令:

    SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

    List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();

    for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
        int subscriptionId = subscriptionInfo.getSubscriptionId();
        Log.d("Sims", "subscriptionId:" + subscriptionId);
    }

    if (subscriptionInfoList != null) {
        if (subscriptionInfoList.size() == 1) {
            sim1 = subscriptionInfoList.get(0).getDisplayName().toString();
            tvSim1.setText(sim1);
        } else {
            sim1 = subscriptionInfoList.get(0).getDisplayName().toString();
            sim2 = subscriptionInfoList.get(1).getDisplayName().toString();

            tvSim1.setText(sim1);
            tvSim2.setText(sim2);
        }

    }
于 2017-10-23T13:34:21.003 回答
9

您可以像任何其他号码一样拨打 ussd 请求,其呼叫意图如下:

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

但是,afaik,目前无法在您的应用程序中解析结果字符串。

于 2011-04-01T09:19:16.650 回答
9

您可以拦截 USSD 响应,为此您需要实现 IExtendedNetworkService.aidl 接口,该接口将服务与 PhoneUtils 绑定。然后它可以拦截任何 USSD 响应,您可以轻松地在您的应用程序中读取它。仅供参考 https://github.com/alaasalman/ussdinterceptor

于 2013-01-22T05:59:39.573 回答
4

从 Android O 开始,可以使用TelephonyManager.

于 2017-06-13T06:27:15.893 回答
0

希望它对你有用:

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);
于 2013-07-08T15:03:39.353 回答