3

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.

prince47
  • 41
  • 6

1 Answers1

1

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.

oldcode
  • 1,669
  • 3
  • 22
  • 41
  • I do not know how to handle the call log! Help me please I'm still a beginner! – prince47 Aug 02 '17 at 17:12
  • 1
    @prince47 Here are some [link 1](https://stackoverflow.com/questions/6786666/how-do-i-access-call-log-for-android) , [Link 2](https://stackoverflow.com/questions/17774111/how-to-get-the-number-of-the-most-frequent-value-entry-in-calllog-calls) – oldcode Aug 02 '17 at 17:13
  • Okay. I first test the incoming call and I see the links thank you already – prince47 Aug 02 '17 at 17:18
  • This method: if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"); } do not give at home! – prince47 Aug 02 '17 at 17:38
  • You must register for the intent filter as shown in the above link, – oldcode Aug 03 '17 at 06:04
  • This method works well in the BroadcastReceiver but when I put it in my Service class, it no longer works – prince47 Aug 04 '17 at 06:05
  • if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"); } – prince47 Aug 04 '17 at 06:05
  • @prince47 You need to request appropriate permissions for that.`` this is the permission. – oldcode Aug 05 '17 at 09:44
  • It still does not give! In short I used a SharedPreferences! I do not know if it will disturb me one day? – prince47 Aug 05 '17 at 12:00
  • @prince47 which device are you using ? – oldcode Aug 05 '17 at 12:13