Android PhoneStateListener返回过去的调用

时间:2011-05-25 15:18:24

标签: android broadcastreceiver

我使用PhoneStateListener来查明电话何时处于通话状态,以及何时通话结束并记录其是来电还是来电。当我安装监听器时,它在第一次调用时按预期工作。虽然它仍然有用,但它会返回第一个呼叫以及第二个呼叫的信息。在第3个呼叫时,它返回3个呼叫的信息等。 我如何仅获取当前通话的信息​​?以下代码,任何帮助表示赞赏

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    mIntent = intent;
    mlocManager= (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int events = PhoneStateListener.LISTEN_CALL_STATE;
    tm.listen(phoneStateListener, events);
}


private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        Log.e("GO", incomingNumber);



        String number = null;
        String callState = "UNKNOWN";
        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            callState = "IDLE";
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            // -- check international call or not.
            callState = "INCOMING";
            number = incomingNumber;

            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            number = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

            callState = "OUTGOING";

            break;
        }




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

1 个答案:

答案 0 :(得分:3)

回答我自己的问题。此代码为每个调用创建多个侦听器。我必须在通话结束后用下面的行

停止听众
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);