Search code examples
androidbroadcastreceiverandroid-manifestandroid-permissions

Requesting outgoing call permissions for emergency numbers


I want to create an android application that checks outgoing calls and if it's an emergency number, my broadcast receiver is called. I'm aware of Google play store restrictions and the probability of being blacklisted. But I need to upload the application first to sign their declaration.

There are several similar questions and I have tried all of them. I believe the rules have changed considerably during the last few years.

Problem: I intent to receive the android intent action NEW_OUTGOING_CALL in my broadcast receiver but I am unable to. I'm able to receive other android actions like AIRPLANE_MODE. But for some reason, NEW_OUTGOING_CALLS are not received in my receiver (Tried API 28-33). I've tried simple toast messages and debugger breakpoints to see if onReceive is triggered but it only triggers for AIRPLANE_MODE and not NEW_OUTGOING_CALL.

Manifest

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

<receiver android:name=".ConnectionReceiver" android:enabled="true" android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

ConnectionReceiver.kt

class ConnectionReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "android.intent.action.NEW_OUTGOING_CALL") {
            //Toast.make.... <--- Just a test toast here
        }
    }
}

Expectation: The toast message should be shown or breakpoint must be hit

Do I need to add other permissions (like MANAGE_OWN_CALLS)? Or is this completely prohibited in 2023? Tried on emulator Nexus API 28, Pixel 4 API 33


Solution

  • I have made a similar application , I am sharing my code hope it helps you

    BroadcastReceiver

    public class OutgoingCallReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
                TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
                if (tm.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
    
                    String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    
                }
    
        }
    
    }
    

    Manifest.xml

     <uses-permission android:name="android.permission.NEW_OUTGOING_CALL" />
        <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    

    Requesting permission at runtime

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS, Manifest.permission.READ_PHONE_STATE},
                        1);
            }
    
    

    I do not have the have the emergency number functionality here But researched and found something from the developers blog


    Note that the system broadcasts NEW_OUTGOING_CALL only for numbers that are not associated with core dialing capabilities such as emergency numbers. This means that NEW_OUTGOING_CALL can not interfere with access to emergency services the way your use of CALL_PRIVILEGED might.

    https://android-developers.googleblog.com/2013/05/handling-phone-call-requests-right-way.html