如何检测,当电话接通或拒绝电话

2023-09-11 12:47:27 作者:暮烟疏雨时光

我要prepare管理活动的时候,手机铃声响起。现在我需要知道如何当我anwser手机取消本活动或我拒绝call.Do我打电话EXTRA_STATE_IDLE?EXTRA_STATE_OFFHOOK?

任何想法?

 <接收机器人:名称=。IncomingBroadcastReceiver>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.PHONE_STATE/>
        &所述; /意图滤光器>
    < /接收器>



公共类IncomingBroadcastReceiver扩展的BroadcastReceiver {

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    字符串状态= intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    //如果来电到达
    如果(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {//做我的工作}
 

解决方案

在你的onReceive:

  PhoneStateChangeListener pscl =新PhoneStateChangeListener;
TelephonyManager TM =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
tm.li​​sten(pscl,PhoneStateListener.LISTEN_CALL_STATE);
 
联通手机可以设置来电拒接吗,而且要对方听到占时无法接通,如果有,怎么设置

单独的类:

 私有类PhoneStateChangeListener扩展PhoneStateListener {
    公共静态布尔wasRinging;
    @覆盖
    公共无效onCallStateChanged(INT状态,串incomingNumber){
        开关(州){
            案例TelephonyManager.CALL_STATE_RINGING:
                 Log.i(LOG_TAG,振铃);
                 wasRinging = TRUE;
                 打破;
            案例TelephonyManager.CALL_STATE_OFFHOOK:
                 Log.i(LOG_TAG,摘机);

                 如果(!wasRinging){
                     //启动新活动
                 } 其他 {
                     //取消旧的活动
                 }

                 //这应该是最后一块code在中场休息前
                 wasRinging = TRUE;
                 打破;
            案例TelephonyManager.CALL_STATE_IDLE:
                 Log.i(LOG_TAG空转);
                 //这应该是最后一块code在中场休息前
                 wasRinging = FALSE;
                 打破;
        }
    }
}
 

所有你需要做的是写一些code,以检查previous状态是'响'。 如果当前状态为空闲状态和previous状态里响起,他们取消了电话。 如果当前状态是摘机和previous状态里响起,他们响应号召。

I managed to prepare an activity when the phone is ringing. Now I need to know how to cancel this activity when I anwser the phone or I reject the call.Do I call EXTRA_STATE_IDLE?EXTRA_STATE_OFFHOOK?

Any ideas?

<receiver android:name=".IncomingBroadcastReceiver">                     
        <intent-filter>                                                   
            <action android:name="android.intent.action.PHONE_STATE"/>   
        </intent-filter>
    </receiver> 



public class IncomingBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    // If an incoming call arrives
    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    { //Did my work }

解决方案

in your onReceive:

PhoneStateChangeListener pscl = new PhoneStateChangeListener;
TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(pscl, PhoneStateListener.LISTEN_CALL_STATE);

separate class:

private class PhoneStateChangeListener extends PhoneStateListener {
    public static boolean wasRinging;
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                 Log.i(LOG_TAG, "RINGING");
                 wasRinging = true;
                 break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                 Log.i(LOG_TAG, "OFFHOOK");

                 if (!wasRinging) {
                     // Start your new activity
                 } else {
                     // Cancel your old activity
                 }

                 // this should be the last piece of code before the break
                 wasRinging = true;
                 break;
            case TelephonyManager.CALL_STATE_IDLE:
                 Log.i(LOG_TAG, "IDLE");
                 // this should be the last piece of code before the break
                 wasRinging = false;
                 break;
        }
    }
}

All you need to do is write some code to check if the previous state was 'ringing'. If the current state is idle and the previous state was ringing, they cancelled the call. If the current state is offhook and the previous state was ringing, they answered the call.