简体   繁体   中英

Not Getting Response of USSD Code in Android

I want to dial and get the dialed USSD code response. Here is my code. but when I run it doesn't show any response on the toast.

private void ussdResponse(String completeCode) {

    TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

    if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, requestCode);
        ActivityCompat.requestPermissions(MainActivity.this , new String[]{Manifest.permission.READ_PHONE_STATE},requestCode);
        return;
    }

    manager.sendUssdRequest(completeCode, new TelephonyManager.UssdResponseCallback() {
        @Override
        public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {

            super.onReceiveUssdResponse(telephonyManager, request, response);

            Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
            Toast.makeText(MainActivity.this, "USSD Result"+response.toString(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {

            super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);
            Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
            Toast.makeText(MainActivity.this, "USSD Response Failed.", Toast.LENGTH_SHORT).show();
        }

    }, new Handler());
}

compleCode contains the USSD Code. But the Toast shows Nothing. can any one please find me the solution.? Or is there any other way to get the dialed USSD response in my app?

I found the answer myself. I Used this Api to resolve the problem. https://github.com/romellfudi/VoIpUSSD

Here is my code:

HashMap map = new HashMap<>();
                        map.put("KEY_LOGIN",new HashSet<>(Arrays.asList("espere", "waiting", "loading", "esperando")));
                        map.put("KEY_ERROR",new HashSet<>(Arrays.asList("problema", "problem", "error", "null")));

                        final USSDApi ussdApi = USSDController.getInstance(MainActivity.this);
                        ussdApi.callUSSDInvoke("*786#", map, new USSDController.CallbackInvoke() {
                            @Override
                            public void responseInvoke(String message) {
                                // message has the response string data
                                String dataToSend = "data";// <- send "data" into USSD's input text
                                ussdApi.send(dataToSend,new USSDController.CallbackMessage(){
                                    @Override
                                    public void responseMessage(String message) {
                                        // message has the response string data from USSD
                                        Log.d("message", message);
                                    }
                                });
                            }

                            @Override
                            public void over(String message) {
                                // message has the response string data from USSD or error
                                // response no have input text, NOT SEND ANY DATA
                            }
                        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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