简体   繁体   中英

Check if incoming call was missed

I have a broadcast receiver registered for incoming call event states and I want to be sure that the idle incoming call was a Missed Call as oposed to a rejected call. For this I access to the CallLog provider to get the latest device call and I compare it's type with CallLog.Calls.MISSED_TYPE. The problem is that my receiver does this before the CallLog provider is updated with the last call that was received by the device... That is why I'm using Thread.sleep on the receiver, to make my receiver wait for the CallLog provider to be update before he queries it, as you can see below:

private boolean isMissedCall(Context context){
    //previous state is a global var   
    if(previousState != null && previousState.equals(RINGING)){

       //THIS IS UGLY!!
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String callType = android.provider.CallLog.Calls.TYPE;
        String callNew = android.provider.CallLog.Calls.NEW;
        String callDate = android.provider.CallLog.Calls.DATE;
        String[] strFields = { callType, callNew, callDate};

        String strOrder = callDate + " DESC";

        Cursor mCallCursor = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, 
                strFields, 
                null, 
                null, 
                strOrder);

        if (mCallCursor.moveToFirst()) {
            if(mCallCursor.getInt(mCallCursor.getColumnIndex(callNew)) > 0) {
                return mCallCursor.getInt(mCallCursor.getColumnIndex(callType)) == CallLog.Calls.MISSED_TYPE;
            }
        }
    }
    return false;
}

I am not happy with the solution of having to put the thread on sleep, but I didnt find anywhere, so far, another solution for the problem. I feel that there must be a beeter way to do this so, I ask you for the best ways you know on how to get the most recent incoming call from the CallLog, on the onReceive method of a bradcast receiver.

PS: my min target Android sdk is 8.

Thanks in advance

I found a blog post which might answer your question as well.

to detect a missed call check be below part in the mentioned post

    case TelephonyManager.CALL_STATE_IDLE:
        //Went to idle-  this is the end of a call.  What type depends on previous state(s)
        if(lastState == TelephonyManager.CALL_STATE_RINGING){
            //Ring but no pickup-  a miss
            onMissedCall(context, savedNumber, callStartTime);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM