I'm doing a program to send the CallLog to a Server, I created a Service that makes a poll of the telephone status with the following method. This method is called onCreate.
private void pollForCallStatus() {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Log.d("Poll", Integer.toString(telephonyManager.getCallState()));
switch (telephonyManager.getCallState()){
case TelephonyManager.CALL_STATE_RINGING:
Log.d("CallService", "ringing");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
callStart();
break;
case TelephonyManager.CALL_STATE_IDLE:
callEnd();
break;
}
}
}, 0, 1000);
}
The method CallEnd, check if the call has started, if its the case its get the duration of this call of the CallLog, I use CallLog because i can't determinate when the outgoing calls starts.
private void callEnd() {
if (isStart) {
Log.d("CallService", "callEnd");
Integer duration = getDuration();
Log.d("CallService", duration.toString());
Command sender = new SendCall(this.phoneNumber, duration);
isStart = false;
this.stopSelf();
}
}
I pasted the method getDuration, I used it to get the duration of the call in CallLog.
private Integer getDuration(){
Integer duration = 0;
callLogCursor.moveToFirst();
if ( phoneNumber.equalsIgnoreCase(callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls.NUMBER))) )
duration = callLogCursor.getInt(callLogCursor.getColumnIndex(CallLog.Calls.DURATION));
return duration;
}
My problem is that getDuration dont get me the last call duration, despite that i recive the previous call. I tried to wait more time but i receive the same result.
--- Edit ---
I changed the code to use ContentObserver, but i have the same result. I create the Observer on BroadcastReceiver
public class CallsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
ContentResolver cr = context.getContentResolver();
Cursor callLogCursor = cr.query(android.provider.CallLog.Calls.CONTENT_URI,
null,
null,
null,
android.provider.CallLog.Calls.DEFAULT_SORT_ORDER /*sort by*/);
Command sender = new SendCall(phoneNumber, callLogCursor);
context.getContentResolver().
registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI,
true, new CallObserver(context, sender));
}
}
When CallLog change executes the method getDuration, the new getDuration is
private Integer getDuration(){
Integer duration = 0;
callCursor.moveToFirst();
if ( phoneNumber.equalsIgnoreCase(callCursor.getString(callCursor.getColumnIndex(CallLog.Calls.NUMBER))) )
duration = callCursor.getInt(callCursor.getColumnIndex(CallLog.Calls.DURATION));
Log.d("SendCall.getDuration", duration.toString());
return duration;
}
and the duration of the last call continues wrong, this duration is to the previous call. I don't know that i can do it.