Skip to content Skip to sidebar Skip to footer

Call (and Get The Response For) A Ussd Code, In The Background?

I'm wanting to write a widget displays the users' prepay balance, remaining data etc. I'm thinking of automatically calling the USSD code which returns this data (will have to hav

Solution 1:

USSD is not yet supported on Android. There is a feature request for it: http://code.google.com/p/android/issues/detail?id=1285

Solution 2:

You can send a USSD code by below code and also you need to add permission (This should be a run time permission after Marshmallow update)

    <uses-permission android:name="android.permission.CALL_PHONE" />

    Stringcode="*" + Uri.encode("#") + 456 + Uri.encode("#");
    startActivity(newIntent("android.intent.action.CALL", Uri.parse("tel:" + code)));

And you can read USSD codes by AccessibilityService

Here is the example:

Service:

publicclassUSSDServiceextendsAccessibilityService {
    privatestatic final StringTAG = "USSDService";

    @OverridepublicvoidonAccessibilityEvent(AccessibilityEvent event) {
        Log.d(TAG, "onAccessibilityEvent");
        String text = event.getText().toString();
        Log.d(TAG, text);
        }
    }

    @OverridepublicvoidonInterrupt() {

    }

    @OverrideprotectedvoidonServiceConnected() {
        super.onServiceConnected();
        Log.d(TAG, "onServiceConnected");
        AccessibilityServiceInfo info = newAccessibilityServiceInfo();
        info.flags = AccessibilityServiceInfo.DEFAULT;
        info.packageNames = newString[]{"com.android.phone"};
        info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        setServiceInfo(info);
    }
}

AndroidManifest.xml :

<serviceandroid:name=".services.USSDService"android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"><intent-filter><actionandroid:name="android.accessibilityservice.AccessibilityService" /></intent-filter><meta-dataandroid:name="android.accessibilityservice"android:resource="@xml/config_service" /> // created below
</service>

res/xml/config_service.xml :

<?xml version="1.0" encoding="utf-8"?><accessibility-servicexmlns:android="http://schemas.android.com/apk/res/android"android:accessibilityEventTypes="typeAllMask"android:accessibilityFeedbackType="feedbackSpoken"android:accessibilityFlags="flagDefault"android:canRetrieveWindowContent="true"android:description="@string/accessibility_description"android:notificationTimeout="100"android:packageNames="com.cootf.sims"android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity" />

Run the code --> Enable the accessibility through Settings --> Accessibility --> [Your app name] --> Enable. Job Done!

Solution 3:

You can use the following code to call the ussd codes on android phone to check the balance and etc......

String s=calledittext.getText.toString();//calledittext is editText on the //screen   from  which  can get the numberif((s.startsWith("*"))&&(s.endsWith("#"))){
            //if true then it is a USSD call----
            callstring=s.substring(0, s.length()-1);
            callstring=callstring+Uri.encode("#");

    Log.d("CALL TYPE---------->", "USSD CALL"); 
        }else{
            callstring=s;   
    Log.d("CALL TYPE---------->", "Not a USSD CALL");   
    //Intent i=new Intent(android.content.Intent.ACTION_CALL,Uri.parse("tel:"+output.getText().toString()));//startActivity(i);
        }
    Intent i=new Intent(android.content.Intent.ACTION_CALL,Uri.parse("tel:"+callstring));
    startActivity(i);

Solution 4:

Android O(API level 26) adds a sendUssdRequest method to the TelephonyManager, which also includes a callback for getting the result for the request.

The docs.

Solution 5:

I'm not completely sure but I think USSD is not yet supported on android, however you can try this workaround:

startActivity(newIntent("android.intent.action.CALL",Uri.parse("tel:*123" + Uri.encode("#")));

Post a Comment for "Call (and Get The Response For) A Ussd Code, In The Background?"