简体   繁体   中英

Number of outgoing call

Here is the code I use to retrieve the number of the incoming, outgoing call:

if(arg1.getAction().equals("android.intent.action.PHONE_STATE")){

                String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);

                if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                    Log.d(TAG, "Inside Extra state off hook");
                    String number = arg1.getStringExtra(TelephonyManager.EXTRA_PHONE_NUMBER);
                    Log.e(TAG, "outgoing number : " + number);              
                }       

                else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){   
                    Log.e(TAG, "Inside EXTRA_STATE_RINGING");
                    String number = arg1.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    Log.e(TAG, "incoming number : " + number);
                }
                else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                    Log.d(TAG, "Inside EXTRA_STATE_IDLE");
                }   
            }

But TelephonyManager.EXTRA_PHONE_NUMBER gives me an error, so I can not get the number of the outgoing call! In addition to that, I would also like to recover the duration of the call. Please help me.

From this question as mentioned here, How to detect incoming calls, in an Android device?

Action NEW_OUTGOING_CALL will give you the outgoing call number,for incoming call your code will work fine.

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

In addition to that I would also like to recover the duration of the call. please help me

You can get the duration of the calls only from the call log or if you consider the above answer from the link in the onOutgoingCallEnded you can subtract the start and the end times but that again will not get you an exact time because OFF_HOOK is triggered when the call is made not when the call is connected or answered, So better stick to call log for the exact duration.

So if you are using the above code from the link and would like to detect duration as soon as the onOutgoingCallEnded is triggered wait a second adn then read from the call log it will then give the latest entry else you will always get the previous entry from call log.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM