简体   繁体   English

如何从Android的USSD代码获取响应?

[英]How to Get Response from USSD code from Android?

I have written an application that uses ussd code. 我编写了一个使用ussd代码的应用程序。 I want to send a request for a ussd but I don't know how to get the data and save it in a String. 我想发送一个ussd的请求,但我不知道如何获取数据并将其保存在String中。

sample code: 示例代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button) findViewById(R.id.button1);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

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

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    Toast.makeText(getApplicationContext(),
            "USSD: " + requestCode + "  " + resultCode + " ", 1).show();

    if (requestCode == 1) {

        if (resultCode == RESULT_OK) {
            // String result=data.getStringExtra("result");
            String dd = data.toString();
            Toast.makeText(getApplicationContext(), dd, 1).show();
        }

    }

Screenshots application: 截图应用程序: 在此输入图像描述

在此输入图像描述

how to Resolve my Problem? 如何解决我的问题?

Dialing a USSD code from a custom activity is straight forward using a DIAL or CALL intent, but listening to the returned result is not due to Android not having proper support for intercepting USSD calls within the platform, but partial though undocumented support exists within the native dialer application. 从自定义活动拨打USSD代码是直接使用DIAL或CALL意图,但是听取返回的结果并不是因为Android没有适当的支持来拦截平台内的USSD调用,但是在本机中存在部分无证支持拨号申请。

As a start, look at the PhoneUtils class in the Android source code. 首先,请查看Android源代码中的PhoneUtils类。 The link is for 4.0.3 but I believe this partial support has been present since 2.3. 该链接适用于4.0.3,但我相信这部分支持自2.3以来一直存在。

Specifically, looking at line 217, an intent with the name “com.android.ussd.IExtendedNetworkService” is being composed. 具体来说,查看第217行,正在编写名为“com.android.ussd.IExtendedNetworkService”的意图。 So what you need to do is implement your own service that responds to that intent. 所以你需要做的是实现自己的服务来响应这个意图。 The service needs to be implemented according to the IExtendedNetworkService.aidl which is a part of the Android framework. 该服务需要根据IExtendedNetworkService.aidl实现,它是Android框架的一部分。

The aidl exposes several functions but the one we care about is the getUserMessage(text) function in that service. aidl公开了几个函数,但我们关心的是该服务中的getUserMessage(text)函数。 The text is the final value returned from the USSD call. 该文本是USSD调用返回的最终值。

Notes: 笔记:

  • Since the service is binded on by PhoneUtils, then you need to start the service at phone boot up. 由于服务是由PhoneUtils绑定的,因此您需要在手机启动时启动该服务。 It also means that any modification to the service will need a phone reboot. 这也意味着对服务的任何修改都需要重启电话。
  • Returning null from getUserMessage will suppress the dialer from showing the USSD result but there's no way to completely hide the dialer. 从getUserMessage返回null将禁止拨号器显示USSD结果,但无法完全隐藏拨号程序。
  • You can also use the other functions to change the text shown while the call is in progress. 您还可以使用其他功能更改呼叫正在进行时显示的文本。
  • This doesn't seem to work on USSD prompts(menus), only on final results. 这似乎不适用于USSD提示(菜单),仅适用于最终结果。

Checkout an example code on github here . 这里查看github上的示例代码。

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

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