How to determine the number of minutes the user is being in a GSM call?

北慕城南 提交于 2019-12-11 13:56:48

问题


I'm trying to determine for how long a user has been in a GSM call. Is that possible using the TelephonyManager?Thank you.


回答1:


You can set a listener on TelephonyManager. Try this.

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;
        }
    };
};


来源:https://stackoverflow.com/questions/33234147/how-to-determine-the-number-of-minutes-the-user-is-being-in-a-gsm-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!