Search code examples
androidphone-callussd

Is there a means to forcefully autodial special numbers on Android?


I've been working on an android app concept in which the app has to auto-dial some special USSD codes in order to initiate certain telco services of interest to the user when the user initiates the service via a shortcut in the app.

The trouble I'm finding is that when the app tries to auto-dial such short codes or USSD numbers, the phone's OS (or is it the Call Intent), doesn't auto-dial, but instead presents the user with the code/number in the dial-pad and so the user has to manually initiate the call - which sort of defeats my intention of allowing users to initiate the services with just one click - the shortcut.

Currently, this is how I'm initiating these calls:

            intent = new Intent(Intent.ACTION_DIAL);

            intent.setData(Uri.parse("tel:" + number.trim()));

            try {
                activity.startActivity(intent);
            } catch (Exception e) {
                Log.d(Tag, e.getMessage());
            }

Interestingly, a number such as +256772777000 will auto-dial, launching the user into the call automatically, but a number/code such as 911, *112#, *1*23#, etc won't.

So, what do I need to do differently, or is this not possible at all?

UPDATE

Actually, looking at another app in which I was autodialling user-specified numbers, the problem with the above code trying to auto-dial ussd codes was that instead of using intent.ACTION_CALL, I was using intent.ACTION_DIAL - which definitely just prompts the user with the number to call, without directly calling it. When I fixed that, the app now works as expected. See answer below...

Code samples are most welcome.


Solution

  • Actually, despite what some people were claiming about Android preventing such a feature. When I looked at the code in one of my older apps which auto-dials user-specified numbers, I found the solution to be:

                intent = new Intent(Intent.ACTION_CALL);
    
                intent.setData(Uri.parse("tel:" + number.trim()));
    
                try {
                    activity.startActivity(intent);
                } catch (Exception e) {
                    Log.d(Tag, e.getMessage());
                }
    

    This works as expected - USSD codes get auto-dialled when above code runs. The only important thing to note when using this approach, being that you have to add the following permissions to your manifest:

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

    So, as indicated in the update to my question, the problem with my original approach was using intent.ACTION_DIAL instead of intent.ACTION_CALL.