简体   繁体   English

BroadcastReceiver中的传入和传出呼叫

[英]Incoming and outgoing calls in the BroadcastReceiver

There is a service in which the processed media player would want to do that for an incoming and outgoing call service stopped media player, and at the end of the call flow resumes playback. 存在一种服务,其中处理的媒体播放器将希望为传入和传出呼叫服务停止的媒体播放器执行该操作,并且在呼叫流程结束时恢复播放。 Now only managed to stop playback on incoming and outgoing calls, should add code to play TelephonyManager.CALL_STATE_IDLE: then the talking after a while the music starts, how to fix it? 现在只设法停止在传入和传出呼叫上播放,应该添加代码来播放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) {

        }
    }
};
}

Whenever before ringing the call state should be TelephonyManager.CALL_STATE_IDLE , Before get incoming call the Call State should be IDEL, So you have to set a flag boolean to Identify the state; 每当响铃之前,呼叫状态应该是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) {

      }
    }
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM