How to get call state of outgoing android call

3.9k views Asked by At

I am originating a call from Android Studio. The code is as follows: I want to get the state of the call at any point. The link: https://developer.android.com/reference/android/telecom/Call.html shows the state of the call can be obtained by using the Class call.. SO if I use Call.getState() I should be able to get the current state. But I get the compilation error: Error:(28, 20) error: Call() is not public in Call; cannot be accessed from outside package. There are several call states defined in the enum: Dialing, Ringing, Connected, DIsconnected, Holding, etc. When I run the code, it does make the call as I can see the screen of emulator making the call.

The developer guide does not provide any examples of using these classes. Thank you for your help.

package com.example.ramesh.makeacall;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.Call;
import android.telephony.*;

import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Call call;
        call =  new Call();
        call();

    }
    private void call() {

        try {

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:5555551212"));
            System.out.println("====before startActivity====");
            startActivity(callIntent);


        } catch (ActivityNotFoundException e) {
            Log.e("helloAndroid","Call failed",e);
        }
    }

    }
2

There are 2 answers

1
kaustav07 On BEST ANSWER

Try to use like this(Haven't tried it though) -

Call.Callback callback = new Call.Callback() {
    @Override
    public void onStateChanged(Call call, int state) {
        super.onStateChanged(call, state);
        if(state == Call.STATE_RINGING){
            //you code goes here
        }
    }
};
2
squalle0nhart On
 public class MyPhoneStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                handleRinging(incomingNumber);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                handleOffHook();
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                handleIdle();
                break;
        }
        super.onCallStateChanged(state, incomingNumber);
    }
}

and register statelistener :

telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);