USSD

FAMOCO has integrated an API into its code base to send and receive USSD messages programmatically.

Send USSD messages

Create an intent to “android.intent.action.ussd.SEND” with a URI to your USSD service

Be careful:

  • the URI needs to be prefixed by “tel:”
  • the # sign needs to be url-encoded, so it will become %23
    For example, to send USSD request to #100#, the URI will be “tel:%23100%23”

Use startService() with that intent

    String ussd =  "#100#";
    Uri.Builder mmi = new Uri.Builder();
    mmi.scheme("tel");
    mmi.opaquePart(ussd);
    Log.d("USSD", mmi.build().toString());

    Intent intent;
    intent = new Intent("android.intent.action.ussd.SEND", mmi.build());
    startService(intent);
    Log.d("USSD", "intent sent");

You should see something similar to the following text in your log:

03-05 10:52:34.076    2077-2077/com.example.ussdtest D/USSD: tel:%23100%23
03-05 10:52:34.082    2077-2077/com.example.ussdtest D/USSD: intent sent
03-05 10:52:35.828    2077-2077/com.example.ussdtest D/USSD: onReceive
03-05 10:52:35.828    2077-2077/com.example.ussdtest D/USSD: 
    1. 留言信箱
    2. 香港接駁鈴聲
    3. 賬單查詢
    4. 漫遊服務
    5. 密碼登記熱線
    *下一頁
    註︰此服務只限3G月費計劃用戶使用

Receive USSD messages

Create a Broadcast receiver for “android.intent.action.ussd.RECEIVE“. The message will be the extra content of the intent “Intent.EXTRA_TEXT“

Example code:

public class MyUssdReceiver extends BroadcastReceiver {
    public MyUssdReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("USSD", "onReceive");
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String message = extras.getString(Intent.EXTRA_TEXT);
            Log.d("USSD", message);
        }
    }
}