Search code examples
androidandroid-intentbroadcastreceiveractionphone-call

android - cannot receive CALL intent in BroadcastReceiver


I have created a BroadcastReceiver to detect CALL action.

android.intent.action.CALL intent is received when it declared in Activity.

<activity
    android:name="com.xxx.yyy.CallCatcherActivity"
    android:launchMode="singleTop"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:screenOrientation="landscape">

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <action android:name="android.intent.action.CALL" />
        <action android:name="android.intent.action.CALL_BUTTON" />
        <action android:name="android.intent.action.CALL_PRIVILEGED" />
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        <action android:name="android.intent.action.DIAL" />

        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="tel" />
    </intent-filter>
</activity>

But, it doesn't work on BroadcastReceiver or Service.

AndroidManifest.xml

<receiver android:name="PhoneCallListener">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <action android:name="android.intent.action.CALL" />
        <action android:name="android.intent.action.CALL_BUTTON" />
        <action android:name="android.intent.action.CALL_PRIVILEGED" />
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        <action android:name="android.intent.action.DIAL" />

        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="tel" />
    </intent-filter>
</receiver>

PhoneCallListener.java

public class PhoneCallListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("-", intent.getAction() + " received");

        try {
            if (intent.getAction().equals("android.intent.action.CALL")) {
                Log.d("-", "+ CALL action received!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Solution

  • Action CALL and action DIAL are used to start an Activity, these actions are NOT used in broadcast Intents. There are separate Intents used for starting Activity, Service and BroadcastReceivers, and they have nothing to do with each other.

    See How do you receive outgoing call in broadcastreceiver for more details about how to catch Intent.ACTION_NEW_OUTGOING_CALL and how to use the PhoneStateListener to detect changes in call state.

    Also see https://www.codeproject.com/Articles/548416/Detecting-incoming-and-outgoing-phone-calls-on-And for a tutorial on detecting incoming and outgoing calls.