Pramod Yadav
Pramod Yadav

Reputation: 2326

How to get the duration of a call in Android?

I want to know how I can get the call duration in Android, I want to start the timer when user starts a call and then after some specific time say after 5 minutes I want to end the call, so I want to know that how can I get the call duration?

I know I can get this by using the call logs but I have a question, can I get the data in real time according to me when my call will be ended then only the call log will be updated and then only I will be able to get the time, but at time the data is useless for me.

Upvotes: 0

Views: 2289

Answers (1)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

On your activity or service

@Override
public void onCreate()
{
    TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    telManager.listen(new MyPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

class MyPhoneStateListener extends PhoneStateListener
{
    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {
        switch (state)
        {
            //When receiving an incoming call
            case TelephonyManager.CALL_STATE_RINGING:
            // Do whaterver you want
            break;

             // When user initiates a call or rigth after CALL_STATE_RINGING
            case TelephonyManager.CALL_STATE_OFFHOOK:
             // set a timer here
             break;

             // When call ended
             case TelephonyManager.CALL_STATE_IDLE:
        }
    }
}

On your manifest file add

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Upvotes: 1

Related Questions