3

Possible Duplicate:
It's possible to get outgoing call duration during call?

I need to show real-time outgoing call duration.

But I do not know when I should start timer.

I must start when get answer from other side. I tried TelephonyManager.EXTRA_STATE_OFFHOOK -- but it's state is when I press call-button..

OFFHOOK is the state when call is placed. So we should be notified when the call is received.

How can we get that? Can You help me? When I should start count outgoing time?

Thanks

upd..

I think this data produces mobile operator server. And this server return call-duration and current balance after each outgoing call . Maybe It's right?

I can't find any solution, but after each call value of call duration will be stored in db. We can get it by CallLog.Calls.DURATION How this value populate field in db?

Community
  • 1
  • 1
vsvydenko
  • 729
  • 1
  • 9
  • 22
  • did you solve this problem? would also like to know the answer about incoming calls and not just outgoing calls. – android developer Sep 30 '13 at 11:37
  • +android developer No, unfortunately I don't have found correct solution.. I can get duration of the call only when call is finished. – vsvydenko Sep 30 '13 at 14:26

1 Answers1

4

Using a content Observer to listen Call Logs content URI if it changed:

ContentResolver contentResolver = context.getContentResolver();
CallLogObserver mObserver = new CallLogObserver(new Handler(), context);
contentResolver.registerContentObserver(
    Uri.parse("content://call_log/calls"), true, mObserver);

This is your CallLogObserver:

public class CallLogObserver extends ContentObserver {
    private Context context;

    public CallLogObserver(Handler handler, Context context) {
        super(handler);
        this.context = context;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Log.i(TAG, "CallLogs Onhange()");
        try{
            Cursor c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
                    null, null, CallLog.Calls.DATE + " DESC");
            if (c != null) {
                if (c.moveToFirst()) {
                    int type = Integer.parseInt(c.getString(c
                            .getColumnIndex(CallLog.Calls.TYPE)));
                    /*
                     * increase call counter for outgoing call only
                     */
                    if (type == 2){
                        String number = c.getString(c
                                .getColumnIndex(CallLog.Calls.NUMBER));

                        long duration = c.getLong(c
                                .getColumnIndex(CallLog.Calls.DURATION));

                                                    Log.i(TAG, "numer = " + number + " type = " + type + " duration = " + duration);

                                                }
                }
                c.close();
            }
            else
                Log.e(TAG,"Call Logs Cursor is Empty");
        }
        catch(Exception e){
            Log.e(TAG, "Error on onChange : "+ e.toString());
        }

    }
Kevin
  • 41
  • 2
  • This doesn't get the duration in real time, the duration of the call is added to the log after the call is finished. – Hakem Zaied Jul 12 '13 at 12:10
  • This is a decent solution but do not catch all exceptions, this is a really bad practice. `try .. catch` block is unnecessary unless the developer wants to add it explicitly – George Pligoropoulos Mar 11 '14 at 10:17
  • You can replace the hardcoded string `Uri.parse("content://call_log/calls")` with this reference from inside android library itself: `android.provider.CallLog.Calls.CONTENT_URI` – George Pligoropoulos Mar 11 '14 at 11:32