Search code examples
androidtelephonytelephonymanager

How do I abort a call and close the dialer after launching my activity?


As I can remember it, Avast anti-theft lets you set up a "secret pin" that you can dial in order to open up the UI. What happens when I dial it is that the call doesn't go through, then the dialer closes immediately and the UI opens up (or the UI goes above the dialer activity. I forgot).

I somehow implemented this, but instead of the call aborting, it goes to the background (green statusbar, ringing phone, etc.) while my activity goes in front of it.

How do I abort the call while opening up my own activity when I dial a specific number?

Some code on my approach:

Broadcast receiver for calls:

public class OutgoingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        // Match if action is outgoing call.

            Intent i = new Intent();
            i.setAction(HelperClass.ACTION_NEW_OUTGOINGCALL);
            i.setClassName(HelperClass.PACKAGE_NAME, HelperClass.CLASS_NAME_OUTGOING);
            i.putExtra(HelperClass.CONTACT_KEY, "contact");
            i.putExtra(HelperClass.CLIENT_DEVICE_KEY, "DUMMY");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(i);

            // Apparently, this doesn't work
            abortBroadcast();





// --------------- FAILED ATTEMPT ---------------------

//        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
//            TelephonyManager tm = (TelephonyManager) context
//                    .getSystemService(Context.TELEPHONY_SERVICE);
//            try {
//                // Java reflection to gain access to TelephonyManager's
//                // ITelephony getter
//                Log.v(HelperClass.TAG, "Get getTeleService...");
//                Class c = Class.forName(tm.getClass().getName());
//                Method m = c.getDeclaredMethod("getITelephony");
//                m.setAccessible(true);
//                com.android.internal.telephony.ITelephony telephonyService =
//                        (ITelephony) m.invoke(tm);
//            } catch (Exception e) {
//                e.printStackTrace();
//                Log.e(TAG,
//                        "FATAL ERROR: could not connect to telephony subsystem");
//                Log.e(TAG, "Exception object: " + e);
//            }
        }
    }
}

Failed attempt: ITelephony not found. The failed attempt came from https://stackoverflow.com/a/5314372/3979290

UPDATE: Tried to implement, failed. Not sure how to implement: How to hang up outgoing call in Android?

Also viewed: How to abort an outgoing call after fixed seconds?


Solution

  • Update: Adding setResultData(null); to the broadcast receiver worked. It immediately ended the call.

    Sample:

    public class OutgoingCallReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Immediately cancels the call since this sets the number to null.
            setResultData(null);
    
            // Launch activity here/other stuff you want to do
            // Example:
            //     Intent i = new Intent(context, MyActivity.class);
            //     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //     context.startActivity(i);
        }
    }
    

    Manifest:

    ...
            <receiver
                android:name=".OutgoingCallReceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter android:priority="2147483647">
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                </intent-filter>
            </receiver>
    ...