8

Is it possible to utilize the users phone through their cell provider, and track the length of a phone call?

So the user presses a button in the app "Call Now". A call begins to a pre-determined number. We record the start time. When the call ends, we calculate how many minutes were used.

Possible?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
spentak
  • 4,627
  • 15
  • 62
  • 90
  • If they start and end the call in your app, can't you just check the current time before and after the call? – Rob Lourens Apr 05 '12 at 16:43
  • That is what i'm trying to figure out. Does the iOS and Android platforms allow me to detect when a call has ended? – spentak Apr 05 '12 at 16:46
  • You can get call logs on Android and iOS, or if you're only concerned about calls placed from the app as you describe, then you can check the system time. – Rob Lourens Apr 05 '12 at 16:50
  • 1
    The answer is going to be _wildly_ different for each of these platforms -- there's no way that code applicable to one is even going to be portable, let alone run. That being the case, and since you've already recieved and accepted an Android answer, I've removed the iOS tags. – jscs Apr 05 '12 at 17:30

2 Answers2

11

To calculate time talked for both incoming , outgoing calls use the following broadcast receiver :

public class CallDurationReceiver extends BroadcastReceiver {

    static boolean flag = false;
    static long start_time, end_time;

    @Override
    public void onReceive(Context arg0, Intent intent) {
        String action = intent.getAction();
        if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_RINGING)) {
                start_time = System.currentTimeMillis();
            }
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_IDLE)) {
                end_time = System.currentTimeMillis();
                //Total time talked =
                long total_time = end_time - start_time;
                //Store total_time somewhere or pass it to an Activity using intent
            }
        }
    }

Register your receiver in your manifest file like this:

 <receiver android:name=".CallDurationReceiver">
       <intent-filter>
           <action android:name="android.intent.action.PHONE_STATE" />
       </intent-filter>
    </receiver>

Also add the uses permission:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
Akhil
  • 13,888
  • 7
  • 35
  • 39
  • 11
    Though, computing the duration like this will include the pick up time, resulting in an estimate for excess of the real duration. I'm searching for a solution to this too, and it seems like the call log is the only place where you can find the precise duration information. – Riccardo T. Jul 13 '13 at 16:31
  • 1
    @Akhil I dont know if this will give accurate results because TelephonyManager.EXTRA_STATE_RINGING is where u get your start time and that automatically means even a missed call will have a duration. I would have thought 'CALL_STATE_OFFHOOK' is the approriate place to start recording duration – Zidane Jul 28 '16 at 12:42
0

This may be too late but just for anyone who may need it in the future. For outgoing calls, you can read the duration from the CallLog. For incoming calls, you can calculate the duration based on the start and end times of the call.

AAK
  • 21
  • 2