【问题标题】:distinguish between answered and unanswered outgoing calls区分已接听和未接听的拨出电话
【发布时间】:2017-07-29 20:29:34
【问题描述】:

我想知道拨出电话是否已被接听。不幸的是,在拨出电话的情况下,我看到了

Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } extras:Bundle{ state => OFFHOOK; }Bundle
Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } extras:Bundle{ state => IDLE; }Bundle

无论是否接听电话。

我如何检测拨出电话是否已接听? (对于有根设备和无根设备,请。)

【问题讨论】:

    标签: android android-intent android-contentprovider contentobserver


    【解决方案1】:

    在 Android 中,您无法使用意图区分已接听和未接听的拨出电话(但在 ios 上可用)。

    我的解决方法:查看手机应用日志(see CallLog.Calls),如果拨出电话的时间日志大于0,则已接听。

        public static final int TIME_TO_WAIT_PHONE_LOG_UPDATE_MILISECOND = 2000;
        public static final int PHONE_CALL_SUCCESS = 0x00; // answered
        public static final int PHONE_CALL_FAILED = 0x01; // unanswered
    

    //

    private class PhoneCallListener extends PhoneStateListener {
    
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                        // Calling not picked up
                        // Đang gọi chưa nhấc máy
                        Log.i(DEBUG_TAG, "OFFHOOK, num = " + incomingNumber);
                        isCalling = true;
                    }
    
                    if (TelephonyManager.CALL_STATE_IDLE == state) {
                        if (isCalling) {
                            // close
                            // Kết Thúc
                            Log.v(DEBUG_TAG, "CALL_STATE_IDLE, num = " + incomingNumber);
                            isCalling = false;
    
                            // Read the log to see if the other party can hear the machine or not
                            // Đọc log xem thử bên kia có nghe máy hay không
                            new Handler().postDelayed(new Runnable() {
    
                                @Override
                                public void run() {
                                    int result = getCallDetails();
    
                                    if (mOnCallFinishListener != null) {
                                        mOnCallFinishListener.onCallFinish(supporter, result);
                                    }
                                }
                            }, MikeMikeConfig.TIME_TO_WAIT_PHONE_LOG_UPDATE_MILISECOND);
                        }
                    }
                }
            }
    
    private int getCallDetails() { 
                int ret = 0;
    
                StringBuffer sb = new StringBuffer();
                Cursor managedCursor = activity.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, /*null*/ CallLog.Calls.DEFAULT_SORT_ORDER);
                // on some phones (e.g. LG) null does not mean DEFAULT_SORT_ORDER which is "date DESC"
                int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER); 
                int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
                int date = managedCursor.getColumnIndex(CallLog.Calls.DATE); 
                int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION); 
                sb.append("Call Log :"); 
                //while (managedCursor.moveToNext()) { 
                    managedCursor.moveToNext();
                    String phNumber = managedCursor.getString(number);
                    String callType = managedCursor.getString(type); 
                    String callDate = managedCursor.getString(date); 
                    Date callDayTime = new Date(Long.valueOf(callDate)); 
                    String callDuration = managedCursor.getString(duration); 
                    String dir = null; int dircode = Integer.parseInt(callType);
                    switch (dircode) { 
                    case CallLog.Calls.OUTGOING_TYPE:
                        dir = "OUTGOING"; 
                        break; 
                    case CallLog.Calls.INCOMING_TYPE: 
                            dir = "INCOMING"; 
                            break;
                    case CallLog.Calls.MISSED_TYPE: 
                        dir = "MISSED"; 
                        break; 
                    } 
    
                    sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration); sb.append("\n----------------------------------");
                //} //managedCursor.close(); textView.setText(sb); } 
    
                //Log.v(DEBUG_TAG, sb + "");
    
                    int call_duration = Integer.parseInt(callDuration);
                    Log.v(DEBUG_TAG, "duration = " + call_duration);
    
    
                    if (call_duration > 0) ret = MikeMikeConfig.PHONE_CALL_SUCCESS;
    
                    if (call_duration == 0) ret = MikeMikeConfig.PHONE_CALL_FAILED;
    
                    return ret;
            }
    

    【讨论】:

    • 试过了,效果很好,BUT:在我的手机上最后一次通话是last 在游标的数据集中。我没有理由相信这不是你手机上的第一个。
    • 为了以正确的方式订购呼叫,我在mService.getContentResolver().query(CALLOG_URI, null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER) 中指定了CallLog.Calls.DEFAULT_SORT_ORDER,这是因为DEFAULT_SORT_ORDER 被记录为"date DESC"。绝对不清楚为什么 null 上的默认排序顺序不是 DEFAULT_SORT_ORDER,但确实如此。 (也编辑答案)
    • 再注:我注册了一个ContentObserver,并通过onChange()方法获取光标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多