Get a response from USSD Android Studio

1

I am currently facing the problem of getting the answer from a USSD call, I have researched a bit and found that just from the API 26, OREON, Android I get an api called TelephonyManager.UssdResponseCallback

link

that the code to execute it is like this

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


            Toast.makeText(Login.this,  response, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
            super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);
            Toast.makeText(Login.this,  request, Toast.LENGTH_LONG).show();
        }
    }, new Handler());

Although this as I said is for api 26, I would need something that works from 4.1 onwards, from now I thank any help

    
asked by Bruno Sosa Fast Tag 25.10.2017 в 16:23
source

2 answers

2

For this you can perform a method that works with both API 26 or higher and also with versions prior to API 26:

private final static int MY_PERMISSIONS_REQUEST_CALL_PHONE = 123;

private void requestUSSD(String USSD){

    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
        return;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //API >= 26
        TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        manager.sendUssdRequest(USSD, new TelephonyManager.UssdResponseCallback() {
            @Override
            public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
                super.onReceiveUssdResponse(telephonyManager, request, response);
                Toast.makeText(getApplicationContext(), "onReceiveUssdResponse()" + response, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
                super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);
                Toast.makeText(getApplicationContext(), "onReceiveUssdResponseFailed()" + request, Toast.LENGTH_LONG).show();
            }
        }, new Handler());
    }else{      //API < 26
        Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" +USSD));
        startActivity(callIntent);
    }

}

//Detecta si los permisos fueron concedidos (android 6.0+)
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_CALL_PHONE : {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // PERMISO CONCEDIDO!
            } else {
                Toast.makeText(getApplicationContext(), "No se tienen permisos CALL_PHONE!", Toast.LENGTH_LONG).show();
            }
            return;
        }
    }
}

The response is only supported by API 26 or higher.

  

It is very important to keep in mind that the USSD codes are not supported in   all devices, including some are blocked by the carrier,   so you may have the message "invalid MMI code".

    
answered by 25.10.2017 / 18:04
source
0

The way to resolve was using the Accessibility service

first create the service class

public class XXXX extends AccessibilityService {

    public static String TAG = "XXXX";

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.d(TAG, "onAccessibilityEvent");
        String text = event.getText().toString();

        if (event.getClassName().equals("android.app.AlertDialog")) {
            performGlobalAction(GLOBAL_ACTION_BACK);

            Log.d(TAG, text);
            Intent intent = new Intent("com.times.ussd.action.REFRESH");
            intent.putExtra("message", text);
            Globals.setTEXT(text);
        }

    }

    @Override
    public void onInterrupt() {
    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d(TAG, "onServiceConnected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.flags = AccessibilityServiceInfo.DEFAULT;
        info.packageNames = new String[]{"com.android.phone"};
        info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        setServiceInfo(info);
    }


}

On the Manifest

 <service android:name=".XXXX"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
        <meta-data android:name="android.accessibilityservice"
            android:resource="@xml/config_service" /> // created below
    </service>

in the class where you want to make the request

String USSD = Uri.encode("*") + "611" + Uri.encode("#");
 requestUSSD(USSD);

method:

private void requestUSSD(String USSD){

    if (ActivityCompat.checkSelfPermission(Login.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
        return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //API >= 26
        TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        manager.sendUssdRequest(USSD, new TelephonyManager.UssdResponseCallback() {
            @Override
            public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
                super.onReceiveUssdResponse(telephonyManager, request, response);
                Toast.makeText(getApplicationContext(), "onReceiveUssdResponse()" + response, Toast.LENGTH_LONG).show();
            }
            @Override
            public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
                super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);
                Toast.makeText(getApplicationContext(), "onReceiveUssdResponseFailed()" + request, Toast.LENGTH_LONG).show();
            }
        }, new Handler());
    }else{      //API < 26
        startService(new Intent(this, XXXX.class));
        dailNumber("*611#");
    }

}

dailNumber:

 private void dailNumber(String s) {
    String USSD = Uri.encode("*") + "611" + Uri.encode("#");
    startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + USSD)));
}

config_service:

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackSpoken"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"
android:description="@string/desc"
android:notificationTimeout="100"
android:packageNames="com.times.ussd"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity" />

Important once installed the application is to go to Configuration - Accecibildad - find the application and give accessibility permissions

    
answered by 25.10.2017 в 22:34