Is there any way to know if an outgoing call is ac

2019-06-03 23:36发布

问题:

I'm doing an app that record call (both of them, incoming and outgoing calls).

I've just resolved the problem of recording audio from incoming calls, but I've found some troubles with outgoing calls.

My situation is next. I'd like to record audio only when the call is accepted, but I don't know how to do it. I've just tried using PhoneStateListener class, but the call state doesn't change when the call is accepted. I've next code:

package com.call.record.listeners;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class OutgoingPhoneStateListener extends PhoneStateListener{

    private String phoneNumber;

    public OutgoingPhoneStateListener(String pn) {
        // TODO Auto-generated constructor stub
        super();
        phoneNumber = pn;
    }

    public void onCallStateChanged(int state, String incomingNumber){

        switch(state){
            case TelephonyManager.CALL_STATE_IDLE:
              Log.d("DEBUG", "IDLE");
            break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
              Log.d("DEBUG", "OFFHOOK");
            break;
            case TelephonyManager.CALL_STATE_RINGING:
              Log.d("DEBUG", "RINGING");
            break;
        }
    }
}

In previous code, when a call is outgoing

回答1:

I don't think there's any non-hackish / robust way of finding out when the call has been answered by the other party. CALL_STATE_OFFHOOK and MODE_IN_CALL (AudioManager) will both be set as soon as the call is placed, rather than when the other party answers.

What you could do is to process the recorded downlink data on-the-fly to try to detect when the call is answered. Hopefully the other party will answer with some sort of greeting which you could try to detect by analyzing the audio data to determine when the conversation has begun. Or you could look at the uplink and use the first word you speak into the microphone as a signal that the conversation has started (since you're unlikely to start talking before the other party answers).



回答2:

I completely agree with Michael there is no robust way of finding out when the call has been answered by the calle in android. i.e TelephonyManager. CALL_STATE_OFFHOOK will set as soon as you make a call.

try this example you will understand

public class BroadcastReceiverImpl extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Monitoring The Network !! ", Toast.LENGTH_LONG).show();
        PhoneStateListnerImpl phoneStateListnerImpl = new PhoneStateListnerImpl(context);
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);

            telephonyManager.listen(phoneStateListnerImpl, PhoneStateListener.LISTEN_CALL_STATE);


        }

    private class PhoneStateListnerImpl extends PhoneStateListener{

        private Context mContext;

        public PhoneStateListnerImpl(Context mContext) {
            this.mContext = mContext;
        }


        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            switch (state) {

            case TelephonyManager.CALL_STATE_RINGING:
                 Toast.makeText(mContext, "Phone is Ringng ", Toast.LENGTH_LONG).show();

                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
                 Toast.makeText(mContext, "Phone is Answered ", Toast.LENGTH_LONG).show();

                break;

            case TelephonyManager.CALL_STATE_IDLE:
                 Toast.makeText(mContext, "Call Is Over ", Toast.LENGTH_LONG).show();

                 abortBroadcast();
                break;

            default:
                break;
            }
        }

    }
}

manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pervazive.monitor_network_v08"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name="com.example.broadcastreceiver.BroadcastReceiverImpl"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_PHONE_STATE_CHANGED" />
                <action android:name="android.intent.action.ACTION_NEW_OUTGOING_CALL" />
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
    </application>

</manifest>