Run USSD Code in Android and Keep the App in the firstground
Asked Answered
Z

1

9

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.

Zulema answered 3/8, 2015 at 12:48 Comment(1)
The same question with: #5449964Assemblage
A
3

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);
}
Affectional answered 22/2, 2016 at 7:0 Comment(2)
I don't understand why this answer is accepted. Your code is the same as the OP has written in question. Your code just has more lines.Scandalize
And you could've just used a string builder or even done String.replace("#", Uri.encore("#")) instead..Marsala

© 2022 - 2024 — McMap. All rights reserved.