无法在Android KitKat中获取呼叫详细信息,例如拨出和拨入呼叫的持续时间

时间:2016-06-20 12:01:01

标签: android telephony telephonymanager

我的要求是每当我通过电话号码,持续时间,类型(进/出)和timeOfCall等呼叫信息接听电话时,都会将呼叫详细信息发送到服务器。

然而它在Marshmallow中工作正常,但在KitKat和棒棒糖中也没有,并且该类多次调用。

这是我的代码:

    public class PhoneStateBR extends BroadcastReceiver {

        Context mContext;
        private boolean isOutgoing = false;
        private String phoneNumber;
        int currentState = 0;
        long ringTime = 0;
        long answerTime = 0;
        long endTime = 0;
        long callDuration = 0;
        private DataManager dataManager;
        private String number;

        @Override
        public void onReceive(Context context, Intent intent) {

            mContext = context;
            dataManager = DataManager.getInstance();
            Log.d("PhoneStateBR", "service started-->");

            if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

                isOutgoing = true;
                ringTime = System.currentTimeMillis();
                phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                Log.d("PhoneStateBR", "New outgoing call " + intent);


            } else if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {

                TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
                Log.e("CustomPhoneListener", "Log1");
                CustomPhoneListener customPhoneListener = new CustomPhoneListener();
                telephonyManager.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

            }

        }

        public class CustomPhoneListener extends PhoneStateListener {

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {

                if (incomingNumber != null && incomingNumber.length()>0) {
                    phoneNumber = incomingNumber;
                    Log.e("CustomPhoneListener->", "State" + state + "PhoneNumber->" + phoneNumber);
                    //Incoming call
                    switch (state) {
                        case TelephonyManager.CALL_STATE_RINGING://1

                            isOutgoing = false;
                            currentState = state;
                            ringTime = System.currentTimeMillis();
                            break;

                        case TelephonyManager.CALL_STATE_OFFHOOK://2

                            currentState = state;
                            answerTime = System.currentTimeMillis();
                            endTime = System.currentTimeMillis();
                            callDuration = endTime - answerTime;
                            Log.e("CustomPhoneListener 3", "call answered - " + callDuration);
                            Log.e("CustomPhoneListener", isOutgoing + "," + phoneNumber + "," + callDuration / 1000);
                            sendCallInfo(isOutgoing, phoneNumber, callDuration / 1000, ringTime);
                            break;

                        case TelephonyManager.CALL_STATE_IDLE://0

                            Log.e("CustomPhoneListener", "Idle");
                            if (currentState == TelephonyManager.CALL_STATE_OFFHOOK) {
                                //Call Answered
                                endTime = System.currentTimeMillis();
                                callDuration = endTime - answerTime;
                                Log.e("CustomPhoneListener 3", "call answered - " + callDuration);
                                Log.e("CustomPhoneListener", isOutgoing + "," + phoneNumber + "," + callDuration / 1000);
                                sendCallInfo(isOutgoing, phoneNumber, callDuration / 1000, ringTime);
                            } else if (currentState == TelephonyManager.CALL_STATE_RINGING) {
                                //Call Missed
                                Log.e("CustomPhoneListener", "call missed");
                                Log.e("TAG", isOutgoing + "," + phoneNumber + "," + callDuration / 1000);
                                sendCallInfo(isOutgoing, phoneNumber, callDuration / 1000, ringTime);
                            }
                            break;
                    }
                }
            }
        }

        long sentAnswerTime = -1;

        /****
         * Sending call info to server
         *
         * @param isOutgoing
         * @param phoneNumber
         * @param callDuration
         * @param answerTime
         */
        public void sendCallInfo(boolean isOutgoing, String phoneNumber, long callDuration, long answerTime) {

            Log.e("CustomPhoneListener 4", "isOutgoing" + isOutgoing + "phoneNumber" + phoneNumber + "callDuration" + callDuration + "answerTime" + answerTime);
            if (sentAnswerTime != answerTime) {
                sentAnswerTime = answerTime;

                try {
                    JSONObject json = new JSONObject();
                    json.put("phoneNumber", phoneNumber);
                    json.put("contactName", dataManager.findNameInContacts(mContext, phoneNumber));
                    json.put("interactionType", "VOICE");
                    json.put("commType", isOutgoing ? "OUTGOING" : "INCOMING");
                    json.put("duration", callDuration);
                    json.put("timeInMillis", String.valueOf(answerTime));

                    JSONArray jsonArray = new JSONArray();
                    jsonArray.put(json);

                    //TODO post this to server
                    HashMap<String, String> params = new HashMap<>();
                    params.put("interaction", json.toString());

                    Log.e("CustomPhoneListener 5", "Call History-->" + params.toString());

                    TSNetworkHandler.getInstance(mContext).getResponse(Utility.BASE_URL + "interaction/store", params, new TSNetworkHandler.ResponseHandler() {
                        @Override
                        public void handleResponse(TSNetworkHandler.TSResponse response) {
                            Log.e("CustomPhoneListener 6", "Response " + response.response);
                        }
                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

0 个答案:

没有答案