user2492806
user2492806

Reputation: 209

How to determine the number of minutes the user is being in a GSM call?

I'm trying to determine for how long a user has been in a GSM call. Is that possible using the TelephonyManager?Thank you.

Upvotes: 1

Views: 136

Answers (1)

ZanoOnStack
ZanoOnStack

Reputation: 409

You can set a listener on TelephonyManager. Try this.

long startCallTime = 0;
private void setTelephonyManagerCallListener(){
    TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(phoneCallStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

private PhoneStateListener phoneCallStateListener = new PhoneStateListener(){
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state){
        case TelephonyManager.CALL_STATE_OFFHOOK:
            startCallTime = System.currentTimeMillis();
            break;
        case TelephonyManager.CALL_STATE_RINGING:
        case TelephonyManager.CALL_STATE_IDLE:
            if (startCallTime>0){
                 long endCallTime = System.currentTimeMillis();
                 long durationInMillis = endCallTime - startCallTime;
                 startCallTime = 0;
            }
            break;
        }
    };
};

Upvotes: 1

Related Questions