STATE_IDLE_RINGING而不是CALL_STATE_OFFHOOK

时间:2014-12-26 08:39:27

标签: android phone-state-listener

我正在制作用于录制语音通话的应用。据我所知,一旦打电话,电话就会转到OFFHOOK。

这应该是正在发生的事情。 但是我的应用程序正在录制,这是我第一次拨打电话。之后,每当我打电话时,手机都会进入RINGING状态。我为所有状态放置了一个Log.i()语句,这样我每次更改时都可以获得电话状态。每当我拨打电话时,手机都会进入RINGING状态。

以下是代码: -

public class CallRecorder extends Service{

private MediaRecorder rec;
private boolean recordStarted;
private File file;
//private TelephonyManager manager;
//private StateListener listener;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    Bundle b = intent.getExtras();
    file = Environment.getExternalStorageDirectory();


    rec = new MediaRecorder();
    rec.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
    rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    rec.setOutputFile(file.getAbsolutePath() + "/TESTRECORD.3gp");
    rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);


    TelephonyManager manager = (TelephonyManager) getApplicationContext().getSystemService(getApplicationContext().TELEPHONY_SERVICE);
    manager.listen(new PhoneStateListener(){
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if(TelephonyManager.CALL_STATE_OFFHOOK==state && rec == null)
            {
                try {
                    Log.i("OUTGOING CALL", "CALL_STATE_OFFHOOK");
                    rec.prepare();
                    //Thread.sleep(500);
                    rec.start();
                    recordStarted = true;
                    Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 

            }
            else if(TelephonyManager.CALL_STATE_IDLE==state)
            {
                Log.i("OUTGOING CALL", "CALL_STATE_IDLE");
                if(rec!=null)
                {
                    try{
                    rec.stop();
                    rec.reset();
                    rec.release();
                    recordStarted = false;
                    stopSelf();
                    }
                    catch (IllegalStateException e) {
                        //Log.e("CALL ERROR",e.getMessage());
                    }// ILLEGALSTATEEXCEPTION WHILE THE CALL ENDS AUTOMATICALLY

                }
            }
            else
            {
                Log.i("OUTGOING CALL", "CALL_STATE_RINGING");   // 2nd time outgoing


            }

        }
        }, PhoneStateListener.LISTEN_CALL_STATE);       // LISTENER REGISTERATION
    Toast.makeText(this, "Calling...", Toast.LENGTH_LONG).show();
    return 0;
}
}

现在,每次拨打电话时," PHONE_STATE_RINGING" 的日志数量都在增加。

第二次打电话时,显示了3次。

我第三次打电话,显示了5次。

第四次我打电话,显示了7次。

这里是logcat。

12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.501: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:45.621: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.051: I/OUTGOING CALL(7614): CALL_STATE_RINGING
12-26 13:53:52.071: I/OUTGOING CALL(7614): CALL_STATE_RINGING

每次尝试后,都会显示 CALL_STATE_IDLE

显示了下面给出的一些随机错误

stop is called in an invalid state:4

任何人都可以告诉我为什么&怎么回事? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您尚未取消订阅此活动,因此全部堆叠起来:)非常简单的修复!

这是我的onReceive方法:

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    mIntent = intent;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

这是我在onCallStateChanged事件结束时使用的行:

((TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE)).listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);

那应该解决它!