android - 无法在 BroadcastReceiver 中接收 CALL 意图

android - cannot receive CALL intent in BroadcastReceiver

我创建了一个 BroadcastReceiver 来检测 CALL 操作。

android.intent.action.CALL 意图在 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>

但是,它不适用于 BroadcastReceiverService

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();
        }
    }
}

Action CALL 和 action DIAL 用于启动 Activity,这些动作不用于广播 IntentIntent用于启动ActivityServiceBroadcastReceiver,它们之间没有任何关系。

有关如何捕获 Intent.ACTION_NEW_OUTGOING_CALL 以及如何使用 PhoneStateListener 检测调用状态变化的更多详细信息,请参阅 How do you receive outgoing call in broadcastreceiver

有关检测来电和去电的教程,另请参阅 https://www.codeproject.com/Articles/548416/Detecting-incoming-and-outgoing-phone-calls-on-And