Ferran Maylinch
Ferran Maylinch

Reputation: 11539

How to make a call in Android and tell whether it was answered

I'd like to make a call to some phone number and, if it was not answered, make a call to an alternative phone number.

Is this possible?

I'm trying this code, but I always get CALL_STATE_IDLE and CALL_STATE_OFFHOOK first (just when the call is made) and then when the call is done (either because the call was not answered or because it finished) I get again CALL_STATE_IDLE.

TelephonyManager manager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener callListener = new MyCallListener();
manager.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phoneNumber));
activity.startActivity(intent);

class MyCallListener extends PhoneStateListener
{
    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {
        if (TelephonyManager.CALL_STATE_RINGING == state) {
            Log.i(TAG, "RINGING, number: " + incomingNumber);

        } else if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            Log.i(TAG, "OFFHOOK, number: " + incomingNumber);

        } else if (TelephonyManager.CALL_STATE_IDLE == state) {
            Log.i(TAG, "IDLE, number: " + incomingNumber);

        } else {
            Log.i(TAG, "Unexpected call state: " + state);
        }
    }
}

Upvotes: 0

Views: 80

Answers (1)

dawncode
dawncode

Reputation: 628

You cannot detect whether your call has been answered or not.

The TelephonyManager.CALL_STATE_RINGING is for incoming call not for out going call.

follow this

public static final int CALL_STATE_RINGING

Added in API level 1
Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.

Constant Value: 1 (0x00000001)

Upvotes: 1

Related Questions