How to detect incoming calls, in an Android device?

前端 未结 10 1705
一向
一向 2020-11-22 02:27

I\'m trying to make an app like, when a call comes to the phone I want to detect the number. Below is what I tried, but it\'s not detecting incoming calls.

I want t

10条回答
  •  野的像风
    2020-11-22 02:58

    @Gabe Sechan, thanks for your code. It works fine except the onOutgoingCallEnded(). It is never executed. Testing phones are Samsung S5 & Trendy. There are 2 bugs I think.

    1: a pair of brackets is missing.

    case TelephonyManager.CALL_STATE_IDLE: 
        // Went to idle-  this is the end of a call.  What type depends on previous state(s)
        if (lastState == TelephonyManager.CALL_STATE_RINGING) {
            // Ring but no pickup-  a miss
            onMissedCall(context, savedNumber, callStartTime);
        } else {
            // this one is missing
            if(isIncoming){
                onIncomingCallEnded(context, savedNumber, callStartTime, new Date());                       
            } else {
                onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());                                               
            }
        }
        // this one is missing
        break;
    

    2: lastState is not updated by the state if it is at the end of the function. It should be replaced to the first line of this function by

    public void onCallStateChanged(Context context, int state, String number) {
        int lastStateTemp = lastState;
        lastState = state;
        // todo replace all the "lastState" by lastStateTemp from here.
        if (lastStateTemp  == state) {
            //No change, debounce extras
            return;
        }
        //....
    }
    

    Additional I've put lastState and savedNumber into shared preference as you suggested.

    Just tested it with above changes. Bug fixed at least on my phones.

提交回复
热议问题