0

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36

1 Answers1

1

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"/>
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54