how to get the state for outgoing calls

女生的网名这么多〃 提交于 2019-12-21 06:06:37

问题


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.


回答1:


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



回答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" />



回答3:


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!



来源:https://stackoverflow.com/questions/10213659/how-to-get-the-state-for-outgoing-calls

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