Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run USSD Code in Android and Keep the App in the firstground

Tags:

java

android

ussd

I am creating a App in Android, which required run USSD Code in background. without send my application in background,

Whenever I am using Intent.ACTION_CALL to run USSD

String ussdCode = "*" + "123" + Uri.encode("#");
startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode)));

it send my application in background, and open dialer Interface on my application.

So it is possible to run USSD code without open Dialer Interface in front.

Thanks.

like image 944
Sahlo Avatar asked Aug 03 '15 12:08

Sahlo


People also ask

How to dial USSD code in Android?

This app can be found on your home screen, on the "All Apps" screen, or, on simpler cell phones, on the lock screen. Dial the USSD code. Some start with *, others #, and others *#. Dial #.

What are Ussd apps?

USSD applications run on the network, not on a user's device. As such, they don't have to be installed on the user's phone, which is an advantage for users with feature phones that have limited storage space. USSD apps are instantly available to every subscriber the moment they're deployed to a network.


1 Answers

Use following code:

String ussdCode = "*123#";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(ussdToCallableUri(ussdCode));
try{
    startActivity(intent);
} catch (SecurityException e){
    e.printStackTrace();
}

Here is the method to convert your ussd code to callable ussd:

private Uri ussdToCallableUri(String ussd) {

    String uriString = "";

    if(!ussd.startsWith("tel:"))
        uriString += "tel:";

    for(char c : ussd.toCharArray()) {

        if(c == '#')
            uriString += Uri.encode("#");
        else
            uriString += c;
    }

    return Uri.parse(uriString);
}
like image 112
zeeali Avatar answered Oct 16 '22 15:10

zeeali