get last Call on CallLog using a Service

假如想象 提交于 2019-12-22 01:17:23

问题


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.


回答1:


Add a ContentObserver on call log which will notify you when anything is changed in callLog i.e. if new call is added to call log or you removed the call log

EDIT

Try this get new call log after call finishes I am getting right result with this code

Wait for 3-4 seconds after call finish and then

CursorLoader cursorLoader = new CursorLoader(mActivity,
            CallLog.Calls.CONTENT_URI, null, null, null, null);
managedCursor = cursorLoader.loadInBackground();


int durationIndex = managedCursor.getColumnIndex(CallLog.Calls.DURATION);

managedCursor.moveToFirst();

String duration =managedCursor.getString(duration)


来源:https://stackoverflow.com/questions/22068418/get-last-call-on-calllog-using-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!