简体   繁体   English

Android PhoneStateListener 返回过去的呼叫

[英]Android PhoneStateListener returns past calls

Im using a PhoneStateListener to find out when the phone is in an active call and when the call ends and log wether it was in incoming or outgoing call.我使用 PhoneStateListener 来找出电话何时处于活动呼叫中以及呼叫何时结束并记录它是在呼入还是呼出。 when I install the listener it works as expected on the first call.当我安装监听器时,它在第一次调用时按预期工作。 Affter that although it still works it returns info for the fist call as well as the second call, On the 3rd call it returns info for 3 calls etc. How do I only get info for the active call?之后虽然它仍然有效,但它会返回第一个呼叫和第二个呼叫的信息,在第三个呼叫它返回 3 个呼叫的信息等。我如何只获取活动呼叫的信息? Code below, any help is appreciated下面的代码,任何帮助表示赞赏

@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);
    }
};

Answering my own question.回答我自己的问题。 This code creates multiple listeners for every call.此代码为每个调用创建多个侦听器。 I have to stop the listener after the call ends with the below line通话以以下行结束后,我必须停止侦听器

tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);

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

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