有没有办法知道是否接受拨出电话?

时间:2012-07-26 11:34:38

标签: android call audio-recording phone-state-listener

我正在做一个记录通话的应用程序(它们都是来电和去电)。

我刚解决了来电录制音频的问题,但我发现传出来电时遇到了一些麻烦。

我的情况是下一个。我只想在接听电话时录制音频,但我不知道该怎么做。我刚刚尝试使用PhoneStateListener类,但是当接受调用时,调用状态不会改变。我接下来是代码:

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

在之前的代码中,当呼叫是传出时

2 个答案:

答案 0 :(得分:0)

我认为没有任何非黑客/强有力的方法可以找出对方何时接听电话。一旦发出呼叫,CALL_STATE_OFFHOOKMODE_IN_CALLAudioManager)都将被设置,而不是在另一方回答时设置。

您可以做的是即时处理记录的下行链路数据,以尝试检测呼叫何时应答。希望另一方能够通过分析音频数据来确定您可以尝试检测的某种问候语,以确定会话何时开始。或者您可以查看上行链路,并将您在麦克风中说出的第一个单词用作对话已启动的信号(因为您不太可能在对方回答之前开始说话)。

答案 1 :(得分:-1)

我完全赞同迈克尔,没有强有力的方法可以找出android中的calle何时回应了这个电话。即TelephonyManager。您拨打电话后会立即设置 CALL_STATE_OFFHOOK

试试这个例子,你会理解

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>