I have a activity which is brought in front whenever there are any outgoing call. I need the outgoing call duration in real time and show it in my activity textview. Is this possible to get the current duration if call is in offhook?
Asked
Active
Viewed 6,542 times
2 Answers
3
This can be done. You just have to Spy on Android Call Log Content Provider
. To use a content provider, you just need to call the getContentResolver()
method. Once you have a content resolver object, you can query it using SQL or any of the built in query functions. For example, in my Android App, I use a piece of code that looks like this:
String[] strFields = {
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.TYPE,
android.provider.CallLog.Calls.CACHED_NAME,
android.provider.CallLog.Calls.CACHED_NUMBER_TYPE,
android.provider.CallLog.Calls.DURATION
};
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor mCallCursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI,
strFields,
null,
null,
strOrder
);

latsha
- 1,298
- 1
- 14
- 22

The Dark Knight
- 5,455
- 11
- 54
- 95
-
I try this but i cannot give me solution to getting the real time call duration.. – Deepak Jangir Jun 07 '13 at 11:55
-
When you say real time, do you mean getting the duration , when the call is ongoing or after the call is finished ? – The Dark Knight Jun 07 '13 at 11:56
-
when the call is going...and update it with every call second – Deepak Jangir Jun 07 '13 at 12:00
-
No that's not possible : Check out the comment of Paul Lammertsma in this comment here : http://stackoverflow.com/questions/3959128/its-possible-to-get-outgoing-call-duration-during-call – The Dark Knight Jun 07 '13 at 12:01
-
I can use the timer for how it in textview. and after call disconnecting i will get the actual duration. – Deepak Jangir Jun 07 '13 at 12:04
0
if (callType == OUTGOING_CALL_END || callType == INCOMING_CALL_END) {
Cursor cursorCallLogs = getRecentCallLogs(context.getContentResolver());
if (cursorCallLogs != null) {
cursorCallLogs.moveToLast();
do {
try {
String callNumber = cursorCallLogs.getString(cursorCallLogs.getColumnIndex("number"));
callNumber = utilS.getPhoneNumber(callNumber);
utilS.log("Phone NUmber", callNumber);
duration = (cursorCallLogs.getInt(cursorCallLogs.getColumnIndex("duration")) * 1000);
utilS.log("durationCall", "" + duration);
} catch (Exception e) {
e.printStackTrace();
}
}
while (cursorCallLogs.moveToPrevious());
}
}
public static Cursor getRecentCallLogs(ContentResolver cr) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
return null;
} else {
return cr.query(CallLog.Calls.CONTENT_URI, null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 1");
}
}

Brinda Rathod
- 2,693
- 1
- 20
- 32