天天看点

android 通话状态

思路:就是一个监听,很简单

代码如下你自己试试:

private class EndCallListener extends PhoneStateListener {

            boolean flag =false ;

        @Override

        public void onCallStateChanged(int state, String incomingNumber) {

                //铃声状态

                AudioManager aui =(AudioManager) CallActivity.this.getSystemService(Context.AUDIO_SERVICE);

                switch(state)

                {

                case TelephonyManager.CALL_STATE_IDLE://闲置或结束电话

                        aui.setMode(AudioManager.RINGER_MODE_NORMAL);

                        if(flag){

                                flag= false ;

                                Toast.makeText(CallActivity.this, "通话结束", Toast.LENGTH_LONG).show();

                        }else{

                                Toast.makeText(CallActivity.this, "闲置", Toast.LENGTH_LONG).show();

                        }

                        break;

                case TelephonyManager.CALL_STATE_OFFHOOK://通话中

                        flag= true ;

                        Toast.makeText(CallActivity.this, "通话中", Toast.LENGTH_LONG).show();

                        break ;

                case TelephonyManager.CALL_STATE_RINGING://来电

                        Toast.makeText(CallActivity.this, "来电", Toast.LENGTH_LONG).show();

                        call_incom(incomingNumber,aui);

                }

        }

在activity的onCreate中:

EndCallListener callListener = new EndCallListener();

                TelephonyManager mTM = (TelephonyManager)CallActivity.this.getSystemService(Context.TELEPHONY_SERVICE);

                mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

继续阅读