4

In the OnReceive method I have something like this:

    Bundle bundle=intent.getExtras();
   String phonenumber=intent.getStrngExtra(Intent.EXTRA_PHONE_NUMBER);

How to chech if the dialing call is still on or the client hanged up the call? How to check if the call was answered?

I need to print up a toat when the client hanged up the call or when the called client answered to the call.

user1222905
  • 533
  • 2
  • 17
  • 36

3 Answers3

2

You will need a broadcast receiver registered for action android.intent.action.PHONE_STATE

iF THE phone state has not changed to idle once it is offhook, it means the call is still going on.

the call was answered if the state in read phone state broadcast receiver changes to offhook. Put a toast as need in these states.

   public class CallDurationReceiver extends BroadcastReceiver {

static boolean flag =false;
static long start_time,end_time;    
@Override
    public void onReceive(Context arg0, Intent intent) {
        String action = intent.getAction();
        if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE")){
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                                TelephonyManager.EXTRA_STATE_RINGING)) {

               //tOAST FOR INCOMING CALL, NOT YET PICKED UP

            }         
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                    TelephonyManager.EXTRA_STATE_IDLE)) {
                end_time=System.currentTimeMillis();
 //Total time talked =
                long total_time = end_time-start_time;
                //Store total_time somewhere or pass it to an Activity using intent

}     if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                    TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                 start_time=System.currentTimeMillis();

}    

    }   
    }

Register your receiver in your manifest file like this:

<receiver android:name=".CallDurationReceiver">
       <intent-filter>
           <action android:name="android.intent.action.PHONE_STATE" />
       </intent-filter>
    </receiver>
        }

Also add the uses permission:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Akhil
  • 13,888
  • 7
  • 35
  • 39
  • I need to detect when the outgoing call is canceled or hanged-up. how to check that? – user1222905 Apr 19 '12 at 07:14
  • you can't differentiate between an outgoing/incoming call AFAIK.I gave you the code above to do that. When the state is IDLE, the call is done.so, put all your code in IDLE state if condition. – Akhil Apr 19 '12 at 15:39
2

Well all I find out a solution for this and I successfully implemented it.Its not possible to fetch the exact time when the callee has accepted an outgoing call.

Before picking up a call in the other end it has already passed through 2 stages namely on_State_idle and on_state_offhook. On_state_ringing is not working for the outgoing calls.

Let's assume a phone is ringing for 40sec (this am not sure) continuously if the person at the other side didn't pick the call.

  1. Start a timer along with the starting stage of on_State_idle and on_state_offhook.

  2. Two cases if the timer cross above 40sec means the person at the other hand pick my call.

  3. If on_State_idle->on_state_offhook->on_State_idle worked within 40sec means the other hand didn't pick my call.

  4. If the second case is true, fetch the call talk duration from the call log.

  5. Totaltimer running time - time in call log gives you the exact time of picking of the outgoing Call!

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Sam
  • 335
  • 3
  • 4
  • visit this link http://codingaffairs.blogspot.com/2016/02/how-to-read-precise-state-of-outgoing.html – Developine Feb 10 '16 at 09:26
  • how about getting call duration when you are doing call using sip endpoint to mobile enpoint ? here the call logs doesn't have call duration. – Yogesh Seralia Apr 08 '16 at 12:53
0

you can use the below code for handling call state:::

    private Runnable callMonitor = new Runnable() {
        public void run() {
            try {
                EndCallListener callListener = new EndCallListener();
                TelephonyManager mTM = (TelephonyManager)m_activity.getSystemService(Context.TELEPHONY_SERVICE);
                mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
            } catch(Exception e) {
                Log.e("callMonitor", "Exception: "+e.toString());
            }
        }
    };   

    private class EndCallListener extends PhoneStateListener {
        private boolean active = false;
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if(TelephonyManager.CALL_STATE_RINGING == state) {
                Log.i("EndCallListener", "RINGING, number: " + incomingNumber);
            }
            if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
                //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
                active = true;
                Log.i("EndCallListener", "OFFHOOK");
            }
            if(TelephonyManager.CALL_STATE_IDLE == state) {
                //when this state occurs, and your flag is set, restart your app
                Log.i("EndCallListener", "IDLE");
                if (active) {
                    active = false;

                    // stop listening                   
                    TelephonyManager mTM = (TelephonyManager)m_activity.getSystemService(Context.TELEPHONY_SERVICE);
                    mTM.listen(this, PhoneStateListener.LISTEN_NONE);

                    // restart the inbox activity
//                  Intent intent = new Intent(m_activity, MDInboxActivity.class);
//                  m_activity.startActivity(intent);
                }
            }
        }
    }
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64