Skip to content Skip to sidebar Skip to footer

Android How To Detect If Outgoing Call Is Answered

I'm developing an app that will only be used in house for testing purpose. I have searched a lot and tried different suggestions as suggested in different post but none seems to be

Solution 1:

TRY THIS

Set all required permission in manifest.xml file.

Call this class in Service

publicclassPhoneListenerextendsPhoneStateListener {

privatestaticPhoneListenerinstance=null;

/**
 * Must be called once on app startup
 *
 * @param context - application context
 * @return
 */publicstatic PhoneListener getInstance(Context context) {
    if (instance == null) {
        instance = newPhoneListener(context);
    }
    return instance;
}

publicstaticbooleanhasInstance() {
    returnnull != instance;
}

privatefinal Context context;
private CallLog phoneCall;

privatePhoneListener(Context context) {
    this.context = context;
}

AtomicBooleanisRecording=newAtomicBoolean();
AtomicBooleanisWhitelisted=newAtomicBoolean();


/**
 * Set the outgoing phone number
 * <p/>
 * Called by {@link MyCallReceiver}  since that is where the phone number is available in a outgoing call
 *
 * @param phoneNumber
 */publicvoidsetOutgoing(String phoneNumber) {
    if (null == phoneCall)
        phoneCall = newCallLog();
    phoneCall.setPhoneNumber(phoneNumber);
    phoneCall.setOutgoing();
    // called here so as not to miss recording part of the conversation in TelephonyManager.CALL_STATE_OFFHOOK
    isWhitelisted.set(Database.isWhitelisted(context, phoneCall.getPhoneNumber()));
}

@OverridepublicvoidonCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE: // Idle... no callif (isRecording.get()) {
                RecordCallService.stopRecording(context);
                phoneCall = null;
                isRecording.set(false);
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK: // Call answeredif (isWhitelisted.get()) {
                isWhitelisted.set(false);
                return;
            }
            if (!isRecording.get()) {
                isRecording.set(true);
                // start: Probably not ever usefullif (null == phoneCall)
                    phoneCall = newCallLog();
                if (!incomingNumber.isEmpty()) {
                    phoneCall.setPhoneNumber(incomingNumber);
                }
                // end: Probably not ever usefull
                RecordCallService.sartRecording(context, phoneCall);
            }
            break;
        case TelephonyManager.CALL_STATE_RINGING: // Phone ringing// DO NOT try RECORDING here! Leads to VERY poor quality recordings// I think something is not fully settled with the Incoming phone call when we get CALL_STATE_RINGING// a "SystemClock.sleep(1000);" in the code will allow the incoming call to stabilize and produce a good recording...(as proof of above)if (null == phoneCall)
                phoneCall = newCallLog();
            if (!incomingNumber.isEmpty()) {
                phoneCall.setPhoneNumber(incomingNumber);
                // called here so as not to miss recording part of the conversation in TelephonyManager.CALL_STATE_OFFHOOK
                isWhitelisted.set(Database.isWhitelisted(context, phoneCall.getPhoneNumber()));
            }
            break;
    }

}

}

And use Broadcast Receiver

publicclassMyCallReceiverextendsBroadcastReceiver {

publicMyCallReceiver() {
}

static TelephonyManager manager;

@Override
publicvoidonReceive(Context context, Intent intent) {
    Log.i("JLCreativeCallRecorder", "MyCallReceiver.onReceive ");

    if (!AppPreferences.getInstance(context).isRecordingEnabled()) {
        removeListener();
        return;
    }

    if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) {
        if (!AppPreferences.getInstance(context).isRecordingOutgoingEnabled()) {
            removeListener();
            return;
        }
        PhoneListener.getInstance(context).setOutgoing(intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER));
    } else {
        if (!AppPreferences.getInstance(context).isRecordingIncomingEnabled()) {
            removeListener();
            return;
        }
    }

    // Start Listening to the call....if (null == manager) {
        manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }
    if (null != manager)
        manager.listen(PhoneListener.getInstance(context), PhoneStateListener.LISTEN_CALL_STATE);
}

privatevoidremoveListener() {
    if (null != manager) {
        if (PhoneListener.hasInstance())
            manager.listen(PhoneListener.getInstance(null), PhoneStateListener.LISTEN_NONE);
    }
}

}

I hope you get some help from this code.

Thanks

Solution 2:

You can use accessibility events to detect the call duration and from that you can detect if the outgoing call is answered or not..

I have answered it in detail in Here` you can check that.

Solution 3:

You need this permission in manifest

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

TelephonyManager has a listener to get phone state. implement this tho know if the phone is ringing or in a call.

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        PhoneStateListener callStateListener = newPhoneStateListener() {

            publicvoidonCallStateChanged(int state, String incomingNumber) {
                // TODO React to a incoming call.try {
                    if (state == TelephonyManager.CALL_STATE_RINGING) {
                        Toast.makeText(getApplicationContext(), "Phone Is Ringing" + incomingNumber, Toast.LENGTH_LONG).show();
                        number = incomingNumber;
                        //g.setPhoneNo(incomingNumber);AndroidNetCommunicationClientActivity.mMsgSendRequest("CommandMsgCallIncoming" + number);
                    } elseif (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                        Toast.makeText(getApplicationContext(), "Phone is Currently in A call" + incomingNumber, Toast.LENGTH_LONG).show();
                        //number = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);number = incomingNumber;


                    } elseif (state == TelephonyManager.DATA_DISCONNECTED) {
                        number = "";
                        CallID = "";
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block//conn.MessageBox(MainActivity.this, e.getMessage());
                    e.printStackTrace();
                }
                super.onCallStateChanged(state, incomingNumber);

            }
        };
        telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

Post a Comment for "Android How To Detect If Outgoing Call Is Answered"