简体   繁体   English

检查是否错过来电

[英]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. 为此,我可以访问CallLog提供程序以获取最新的设备调用,并将其类型与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: 问题是我的接收器在用设备接收到的最后一个呼叫更新CallLog提供程序之前执行此操作。这就是为什么我在接收器上使用Thread.sleep来使我的接收器等待CallLog提供程序在他查询之前进行更新,如下所示:

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. 我觉得一定有更好的方法,所以,请您就如何从Bradcast接收器的onReceive方法中从CallLog获取最新来电的最佳方法进行询问。

PS: my min target Android sdk is 8. PS:我的最低目标Android SDK为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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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