使用PhoneStateListener的奇怪行为

时间:2015-02-26 20:59:01

标签: android phone-state-listener

我有一个派生自PhoneStateListener的类,当用CALL_STATE_RINGING调用它时,我会缓存传入的号码。

我注意到有时使用CALL_STATE_RINGING为相同的传入号码调用两次侦听器。

让我感到困惑的是,当监听器被调用两次时,我缓存的传入号码的值为null。这是用于说明的缩写代码:

public class MainActivity extends Activity {
    public  String                  mIncomingCallId;
    private TelephonyManager        mTelephonyMgr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        mTelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
    }

    class TeleListener extends PhoneStateListener {
        public void onCallStateChanged(int state, String incomingNumber) {
            Log.i(TAG, "Incoming number: " + incomingNumber + " mIncomingCallId: " + mIncomingCallId);
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.i(TAG, "CALL_STATE_RINGING");
                    if (mIncomingCallId == null)
                    {
                        Log.i(TAG, "setting mIncomingCallId to: " + incomingNumber);
                        mIncomingCallId = incomingNumber;

执行时,我会在日志中看到这一点:

Incoming number: 2067958520 mIncomingCallId: null
CALL_STATE_RINGING  
setting mIncomingCallId to: 2067958520 onCallStateChanged. 
Incoming number: 2067958520 mIncomingCallId: null  
CALL_STATE_RINGING

当第一次调用onCallStateChanged()时,调用第二次调用onCallStateChanged()时,mIncomingCallId如何为null?

在第二次调用onCallStateChanged()之前,mIncomingCallId未在代码中的其他位置设置为null。

onCreate()只被调用一次。

0 个答案:

没有答案