为什么onCallStateChanged()在Android上调用超过一次在一个单一的电话吗? [英] why onCallStateChanged() on Android is invoked more than one time in a single call?

查看:814
本文介绍了为什么onCallStateChanged()在Android上调用超过一次在一个单一的电话吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个prevents调用,例如防火墙的应用程序。当我调试我的应用程序,我发现,当有在,在 onCallStateChanged()函数调用接口的 PhoneStateListener 被调用三次。其结果是,preventing可导致三个日志一个呼叫。我很困惑!

我的code:

  @覆盖
    公共无效onCallStateChanged(INT状态,弦乐incomingNumber){
        尝试{
            如果(状态== TelephonyManager.CALL_STATE_RINGING&放大器;&安培;
                PhoneUtil.getITelephony(TPM).isRinging()){
                串旗= isBlockCall(myContext,myHelper,myTypes,incomingNumber);
                如果(flag.length()大于0){
                    blockCall();
                    myHelper.insertLog(新的String [] {标志,incomingNumber,将String.valueOf(System.currentTimeMillis的()),空});
                    showNotification(myContext,incomingNumber,System.currentTimeMillis的());
                }
            }
        }赶上(例外五){
            e.printStackTrace();
        }        super.onCallStateChanged(州,incomingNumber);
    }
},PhoneStateListener.LISTEN_CALL_STATE);


解决方案

PinkyJie,

onCallStateChanged()正在与广播接收器。所以onCallStateChanged()总是会被调用时呼叫状态得到改变。有三个呼叫状态。

TelephonyManager.CALL_STATE_IDLE:电话处于空闲状态,没有呼叫行动结果。
TelephonyManager.CALL_STATE_OFFHOOK:至少有一个电话是活跃在任何一种结果。
TelephonyManager.CALL_STATE_RINGING:一个新的呼叫到达,振铃或等待。
TelephonyManager。

系统永远不会点到为止。请参考以下链接的http://developer.android.com/reference/android/telephony/TelephonyManager.html

 公共类TestPhoneStateListener扩展PhoneStateListener {  公共无效onCallStateChanged(INT状态,弦乐incomingNumber){
    // TODO自动生成方法存根
    的System.out.println(aaaaaaaaaaaaaaa ----------------------------------+状态);
    尝试{
        如果(状态== TelephonyManager.CALL_STATE_RINGING){
        }
    }赶上(例外五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    super.onCallStateChanged(州,incomingNumber);
  }
}

I wanted to implement an app that prevents calls like a firewall. When I debug my app, I found that when there is a call in, the onCallStateChanged() function in interface PhoneStateListener is invoked three times. As a result, preventing one call can result in three logs. I'm so confused!!

my code:

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        try {
            if (state == TelephonyManager.CALL_STATE_RINGING && 
                PhoneUtil.getITelephony(tpm).isRinging()) {
                String flag = isBlockCall(myContext, myHelper, myTypes, incomingNumber);
                if (flag.length() > 0) {
                    blockCall();
                    myHelper.insertLog(new String[] { flag, incomingNumber, String.valueOf(System.currentTimeMillis()), null });
                    showNotification(myContext, incomingNumber, System.currentTimeMillis());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        super.onCallStateChanged(state, incomingNumber);
    }
}, PhoneStateListener.LISTEN_CALL_STATE);

解决方案

PinkyJie,

onCallStateChanged() is working with BroadcastReceiver. so onCallStateChanged() will always get called whenever call state get changed. There are three call states.

TelephonyManager.CALL_STATE_IDLE: Phone is in idle mode, no call action.
TelephonyManager.CALL_STATE_OFFHOOK: At least one call is active in any kind.
TelephonyManager.CALL_STATE_RINGING: A new call arrived and is ringing or waiting. TelephonyManager.

system will never go beyond that. please refer this link http://developer.android.com/reference/android/telephony/TelephonyManager.html

public class TestPhoneStateListener extends PhoneStateListener{

  public void onCallStateChanged(int state, String incomingNumber) {
    // TODO Auto-generated method stub
    System.out.println("aaaaaaaaaaaaaaa----------------------------------"+state);
    try {
        if (state == TelephonyManager.CALL_STATE_RINGING ) {
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    super.onCallStateChanged(state, incomingNumber);
  }
}

这篇关于为什么onCallStateChanged()在Android上调用超过一次在一个单一的电话吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆