Run the USSD code in Android and save the application in the first place

I am creating an Android application that requires running USSD code in the background. without sending my application in the background,

Whenever I use Intent.ACTION_CALL to start USSD

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

it sends my application in the background and opens the dialing interface in my application.

Thus, you can run the USSD code without the Dialer open interface.

Thanks.

+5
source share
1 answer

Use the 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 a way to convert your ussd code to ussd called:

 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); } 
+2
source

All Articles