如何确定用户在GSM通话中的分钟数?

时间:2015-10-20 10:24:53

标签: android mobile telephonymanager

我正在尝试确定用户进入GSM通话的时间。可以使用TelephonyManager吗?谢谢。

1 个答案:

答案 0 :(得分:1)

您可以在TelephonyManager上设置侦听器。 试试这个。

long startCallTime = 0;
private void setTelephonyManagerCallListener(){
    TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(phoneCallStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

private PhoneStateListener phoneCallStateListener = new PhoneStateListener(){
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state){
        case TelephonyManager.CALL_STATE_OFFHOOK:
            startCallTime = System.currentTimeMillis();
            break;
        case TelephonyManager.CALL_STATE_RINGING:
        case TelephonyManager.CALL_STATE_IDLE:
            if (startCallTime>0){
                 long endCallTime = System.currentTimeMillis();
                 long durationInMillis = endCallTime - startCallTime;
                 startCallTime = 0;
            }
            break;
        }
    };
};