【问题标题】:How to get call duration starting when the call is answered如何在接听电话时开始通话时间
【发布时间】:2017-05-30 11:46:32
【问题描述】:

我想记录通话时长,但我实现的都是从我按下通话按钮开始的时长。我需要从被叫方接听电话开始的通话时长。

这是我的广播接收器。

public class CallDurationReceiver extends BroadcastReceiver {

private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static long callStartTime;
private static long callEndTime;
private static boolean isIncoming;
private static String savedNumber;


@Override
public void onReceive(Context context, Intent intent) {
    //Log.w("intent " , intent.getAction().toString());

    if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
        savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");

    } else {
        String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
        String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        int state = 0;
        if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            state = TelephonyManager.CALL_STATE_IDLE;
        } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            state = TelephonyManager.CALL_STATE_OFFHOOK;
        } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            state = TelephonyManager.CALL_STATE_RINGING;
        }

        onCallStateChanged(context, state, number);
    }
}


public void onCallStateChanged(Context context, int state, String number) {
    if (lastState == state) {
        //No change, debounce extras
        return;
    }
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            isIncoming = true;
            callStartTime = new Date().getTime();

            savedNumber = number;

            Toast.makeText(context, "Incoming Call Ringing", Toast.LENGTH_SHORT).show();
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //Transition of ringing->offhook are pickups of incoming calls.  Nothing done on them
            if (lastState != TelephonyManager.CALL_STATE_RINGING) {
                isIncoming = false;
                callStartTime = new Date().getTime();
                Toast.makeText(context, "Outgoing Call Started", Toast.LENGTH_SHORT).show();
            }

            break;
        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
                //   Toast.makeText(context, "Ringing but no pickup" + savedNumber + " Call time " + callStartTime + " Date " + new Date(), Toast.LENGTH_SHORT).show();
            } else if (isIncoming) {

                //  Toast.makeText(context, "Incoming " + savedNumber + " Call time " + callStartTime, Toast.LENGTH_SHORT).show();
            } else {
                callEndTime = new Date().getTime();
                long diff = callEndTime - callStartTime;
                long minutes = (diff / 1000) / 60;
                long seconds = (diff / 1000) % 60;
                Toast.makeText(context, savedNumber + " Call time " + minutes + " and " + seconds, Toast.LENGTH_SHORT).show();

            }

            break;
    }
    lastState = state;
} 

【问题讨论】:

    标签: android telephony android-broadcastreceiver telephonymanager


    【解决方案1】:

    使用PhoneStateListener代替使用broadcastreceiver 参考以下: 私有最终 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (DBG) Log.d(LOG_TAG, "PhoneStateListener.onCallStateChanged: state=" + state);
    

    …… }

    状态将是:

     /** Device call state: No activity. */
        public static final int CALL_STATE_IDLE = 0;
        /** Device call state: Ringing. A new call arrived and is
         *  ringing or waiting. In the latter case, another call is
         *  already active. */
        public static final int CALL_STATE_RINGING = 1;
        /** Device call state: Off-hook. At least one call exists
          * that is dialing, active, or on hold, and no calls are ringing
          * or waiting. */
        public static final int CALL_STATE_OFFHOOK = 2;
            }
        }
    };
    

    像下面这样注册这个监听器:

     TelephonyManager tm =
                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    

    【讨论】:

      【解决方案2】:
      @Override
          protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end)
          {
              long milliseconds = end.getTime() - start.getTime();
      
              String duration= String.format("%02d:%02d:%02d",
                      TimeUnit.MILLISECONDS.toHours(milliseconds),
                      TimeUnit.MILLISECONDS.toMinutes(milliseconds),
                      TimeUnit.MILLISECONDS.toSeconds(milliseconds) -
                              TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)));
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多