区分已接听和未接听的拨出电话

时间:2015-03-03 15:55:04

标签: android android-intent android-contentprovider contentobserver

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

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

无论是否接听电话。

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

1 个答案:

答案 0 :(得分: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;
        }