【问题标题】:Incoming and outgoing calls in the BroadcastReceiverBroadcastReceiver 中的来电和去电
【发布时间】:2014-03-19 10:52:36
【问题描述】:

有一项服务,其中已处理的媒体播放器希望为来电和去电服务停止媒体播放器执行此操作,并在呼叫流程结束时恢复播放。 现在只能在呼入和呼出时停止播放,应该添加代码来播放 TelephonyManager.CALL_STATE_IDLE: 然后过一会儿音乐开始说话,如何解决?

public class CallReceiver extends BroadcastReceiver{
TelephonyManager telManager;
Context context;
boolean startedCall = false;

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


this.context=context;


telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

}

private final PhoneStateListener phoneListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);
    try {
        switch (state) {
        case TelephonyManager.CALL_STATE_RINGING: {
            if(incomingNumber!=null)
            {
                //incoming call
                MediaService.stopMP();
                startedCall  = true;
            }

        break;
        }
        case TelephonyManager.CALL_STATE_OFFHOOK: {
            if(incomingNumber!=null)
            {
                //outgoing call
                MediaService.stopMP();
                startedCall  = true;
            }

        break;
        }
        case TelephonyManager.CALL_STATE_IDLE: {
            if(startedCall)
            {
                MainActivity.titleMusic.setVisibility(View.VISIBLE);
                MainActivity.Play();
                MediaService.startMP();
                startedCall = false;
            }                   

        break;
        }
        default: { }
        }
        } catch (Exception ex) {

        }
    }
};
}

【问题讨论】:

    标签: android broadcastreceiver


    【解决方案1】:

    在响铃之前呼叫状态应该是TelephonyManager.CALL_STATE_IDLE,在来电之前呼叫状态应该是IDEL,所以你必须设置一个标志布尔值来识别状态;

        public class CallReceiver extends BroadcastReceiver{
        TelephonyManager telManager;
        Context context;
    
        @Override
        public void onReceive(Context context, Intent intent) {    
           this.context=context;    
           private boolean startedCall = false; // New added boolean    
           telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
           telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);    
        }
    
        private final PhoneStateListener phoneListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        try {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING: {
                    if(incomingNumber!=null) {
                       //incoming call
                       MainActivity.stopMP()
                    }    
                    break;
                }
                case TelephonyManager.CALL_STATE_OFFHOOK: {    
                    startedCall  = true; // Newly added code    
                   if(incomingNumber!=null) {
                      //outgoing call
                       MainActivity.stopMP();
                   }
                   break;
                }
                case TelephonyManager.CALL_STATE_IDLE: {
                   if(startedCall) {
                       MainActivity.titleMusic.setVisibility(View.VISIBLE);
                       MainActivity.Play();
                       MediaService.startMP();
                   }          
                   break;
                }
                default: { }
            }
          } catch (Exception ex) {
    
          }
        }
        };
    }
    

    【讨论】:

    • 拨出电话似乎没有打开,当来电也在对话的背景上播放时,通话再次在中断缓冲区开始。更正了有问题的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2020-10-20
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多