简体   繁体   English

通过BroadcastReceiver监听传入的呼叫,无需PhoneStateIntentReceiver或PhoneStateListener

[英]Listen incoming calls through BroadcastReceiver, without PhoneStateIntentReceiver or PhoneStateListener

Is there any way to listen to incoming calls by extending BroadcastReceiver to listen to OS's broadcast,without using PhoneStateIntentReceiver or PhoneStateListener. 有没有办法通过扩展BroadcastReceiver来监听传入的呼叫来监听OS的广播,而不使用PhoneStateIntentReceiver或PhoneStateListener。 Also please tell me what will be action and permissions in manifest. 另请告诉我清单中的操作和权限是什么。

I've tried with as follows but it is not working for incoming calls but working for outgoing 我已经尝试过如下但是它不适用于来电但是正在工作

The only one .java file of app is as follows(the app only one .java file and one manifest file) 应用程序中唯一一个.java文件如下(app只有一个.java文件和一个清单文件)

package com.crsardar.media.audio;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class IncommingCallReceiverCRS extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("Chitta : ", "Its working");          
    }
}

manifest 表现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.crsardar.media.audio"
      android:versionCode="1"
      android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">

    <receiver android:name="IncommingCallReceiverCRS" android:enabled="true"> 
        <intent-filter>
            <!--action android:name="android.intent.action.NEW_OUTGOING_CALL"/-->
            <action  android:name="android.intent.action.ANSWER" >
            <category  android:name="android.intent.category.DEFAULT" /> 
        </intent-filter>
    </receiver>

  </application>

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

The action you have defined in your manifest is incorrect. 您在清单中定义的操作不正确。 this is an Action Intent that can be used to answer a call and not monitor incoming calls. 这是一个Action Intent,可用于接听电话而不监听来电。

You can use two broadcast receivers that listen to ACTION_PHONE_STATE_CHANGED and NEW_OUTGOING_CALL broadcast intents. 您可以使用两个侦听ACTION_PHONE_STATE_CHANGEDNEW_OUTGOING_CALL广播意图的广播接收器。

The ACTION_PHONE_STATE_CHANGED will be received when there is a new incoming call, call answered or hangup (See the documentation for the EXTRAs received with this Intent). 当有新的来电,呼叫应答或挂断时,将收到ACTION_PHONE_STATE_CHANGED(请参阅本意图收到的EXTRAs的文档)。

The NEW_OUTGOING_CALL will be received when there is a new outgoing call placed on your device. 当您的设备上有新的拨出电话时,将收到NEW_OUTGOING_CALL。

As for permissions, I think you got it about right in your manifest (I assume the RECORD_AUDIO permission is used for something else in your application) 至于权限,我认为你在清单中得到了它(我假设RECORD_AUDIO权限用于你的应用程序中的其他东西)

Here is My demo for android unit test. 这是我的Android单元测试的演示。 You can refer to it. 你可以参考它。

public interface ICallVerify {
    void onOutgoing(Context context, Intent intent);
    void onCallStateChange(Context context, Intent intent);
}

protected void setUpCallVerify(final ICallVerify callVerify) {  //listen ingoing and outgoing
    final CountDownLatch latch = new CountDownLatch(1);
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { //state change
                Log.i(TAG, "outgoing call...");
                callVerify.onOutgoing(context, intent);
            } else if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){ // state changed
                String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                if (state.equals("RINGING")) {
                    state += " number:" + intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                }
                Log.i(TAG, "call state changed.... " + state);
                callVerify.onCallStateChange(context, intent);
            }
        }
    };

    IntentFilter filter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    filter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
    ContextUtils.getTargetContext().registerReceiver(receiver, filter);

    try {
        latch.await(5, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Dont forget to add permissions 别忘了添加权限

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM