0

Is there any state which can tell us that the line is connected and other person's phone is ringing. Like I want to know the state where proper connection is made and other person knows you are calling and can answer it?

I have tried this but it never goes in phone ringing state:

public void onReceive(final Context context, Intent intent) {
TelephonyManagerTm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        Tm.listen(new PhoneStateListener(){
            public void  onCallStateChanged(int state,String number) {
                super.onDataConnectionStateChanged(state);
                switch(state)
                {
                    case TelephonyManager.CALL_STATE_RINGING:
                        Toast.makeText(context, "Phone Ringing", 1).show();
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:

                        break;
                    case TelephonyManager.CALL_STATE_IDLE: 
                        Toast.makeText(context, "call rejected", 1).show();                 
                        break;
                }
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
user2137186
  • 817
  • 6
  • 22
  • 39

3 Answers3

3

Even though there are only three states available in Android Telephony Manager, it is very easy to identify the waiting state of a new incoming call. And this algorithm will provide state for all situations in a phone call.

So here we are receiving the 3 events from android such as RINGING, OFFHOOK and IDLE. And in order to get the waiting state of a new incoming call, we need to define our own states like RINGING, OFFHOOK, IDLE, FIRST_CALL_RINGING, SECOND_CALL_RINGING.
Please think in a way that we are receiving events from android and we will define our on call states.

Here is the method to get call states at onReceive() of broadcast-receiver without registering PhoneStateListener and escaping from other complications. See the code.

public class CallListening  extends BroadcastReceiver {
    static CustomPhoneStateListener customPhoneListener;
    private static final String TAG ="broadcast_intent";
    public static String incoming_number;
    private String current_state,previus_state,event;
    public static Boolean dialog= false;
    private Context context;
    private SharedPreferences sp,sp1;
    private SharedPreferences.Editor spEditor,spEditor1;
    public void onReceive(Context context, Intent intent) {
        //Log.d("intent_log", "Intent" + intent);
        dialog=true;
        this.context = context;
        event = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        incoming_number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.d(TAG, "The received event : "+event+", incoming_number : " + incoming_number);
        previus_state = getCallState(context);
        current_state = "IDLE";
        if(incoming_number!=null){
            updateIncomingNumber(incoming_number,context);
        }else {
            incoming_number=getIncomingNumber(context);
        }
        switch (event) {
            case "RINGING":
                Log.d(TAG, "State : Ringing, incoming_number : " + incoming_number);
            if((previus_state.equals("IDLE")) || (previus_state.equals("FIRST_CALL_RINGING"))){
                    current_state ="FIRST_CALL_RINGING";
                }
                if((previus_state.equals("OFFHOOK"))||(previus_state.equals("SECOND_CALL_RINGING"))){
                    current_state = "SECOND_CALL_RINGING";
                }

                break;
            case "OFFHOOK":
                Log.d(TAG, "State : offhook, incoming_number : " + incoming_number);
                if((previus_state.equals("IDLE")) ||(previus_state.equals("FIRST_CALL_RINGING")) || previus_state.equals("OFFHOOK")){
                    current_state = "OFFHOOK";
                }
                if(previus_state.equals("SECOND_CALL_RINGING")){
                    current_state ="OFFHOOK";
                    startDialog(context);
                }
                break;
            case "IDLE":
                Log.d(TAG, "State : idle and  incoming_number : " + incoming_number);
                if((previus_state.equals("OFFHOOK")) || (previus_state.equals("SECOND_CALL_RINGING")) || (previus_state.equals("IDLE"))){
                    current_state="IDLE";
                }
                if(previus_state.equals("FIRST_CALL_RINGING")){
                    current_state = "IDLE";
                    startDialog(context);
                }
                updateIncomingNumber("no_number",context);
                Log.d(TAG,"stored incoming number flushed");
                break;
        }
        if(!current_state.equals(previus_state)){
            Log.d(TAG, "Updating  state from "+previus_state +" to "+current_state);
            updateCallState(current_state,context);

        }
    }
    public void startDialog(Context context) {
        Log.d(TAG,"Starting Dialog box");
        Intent intent1 = new Intent(context, NotifyHangup.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent1);

    }
    public void updateCallState(String state,Context context){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        spEditor = sp.edit();
        spEditor.putString("call_state", state);
        spEditor.commit();
        Log.d(TAG, "state updated");

    }
    public void updateIncomingNumber(String inc_num,Context context){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        spEditor = sp.edit();
        spEditor.putString("inc_num", inc_num);
        spEditor.commit();
        Log.d(TAG, "incoming number updated");
    }
    public String getCallState(Context context){
        sp1 = PreferenceManager.getDefaultSharedPreferences(context);
        String st =sp1.getString("call_state", "IDLE");
        Log.d(TAG,"get previous state as :"+st);
        return st;
    }
    public String getIncomingNumber(Context context){
        sp1 = PreferenceManager.getDefaultSharedPreferences(context);
        String st =sp1.getString("inc_num", "no_num");
        Log.d(TAG,"get incoming number as :"+st);
        return st;
    }
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
ARUNBALAN NV
  • 1,634
  • 4
  • 17
  • 39
1

Yes we can get the state PhoneStateListener.

Whenever you extend a class from PhoneStateListener, you will get onCallStateChanged(), like below:

 public class CustomPhoneStateListener extends PhoneStateListener {

      ActivityManager activityManager;
      Intent i1;
      public CustomPhoneStateListener(Context context) {
          super();
          this.context = context;
          i1 = new Intent(context, TelephoneyWithoutToastActivity.class);
          i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      }

      @Override
      public void onCallStateChanged(int state, String incomingNumber) {
          super.onCallStateChanged(state, incomingNumber);

          switch (state) {
          case TelephonyManager.CALL_STATE_IDLE:
              //when Idle i.e no call
              Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();

              break;
          case TelephonyManager.CALL_STATE_OFFHOOK:

              //when Off hook i.e in call
              //Make intent and start your service here
              Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();

              break;
          case TelephonyManager.CALL_STATE_RINGING:

              ActivityManager localActivityManager = (ActivityManager) this.context.getSystemService("activity");
              for (String str = ((ActivityManager.RunningTaskInfo) localActivityManager.getRunningTasks(1).get(0)).topActivity.flattenToString();; str = ((ActivityManager.RunningTaskInfo) localActivityManager.getRunningTasks(1).get(0)).topActivity.flattenToString()) {
                  if ((!str.contains("com.android.phone.InCallScreen")))
                      continue;
                  Log.d("IncomingCallPlus", "*****************************************************");   
                  context.startActivity(i1);
                  return;
              }    

          default:
              break;
          }
      }    
  }

For reference check this.

Community
  • 1
  • 1
Pinki
  • 21,723
  • 16
  • 55
  • 88
  • I have edited my question and added my code but I never get in phone ringing condition – user2137186 Oct 21 '13 at 10:13
  • did you mentioned required permissions and broadcast receivers in manifest file – Pinki Oct 21 '13 at 10:19
  • i have following files in manifest: – user2137186 Oct 21 '13 at 10:24
  • did you mentioned the receiver name like – Pinki Oct 21 '13 at 10:39
  • Yes I did that ...I am able to get in idle state and off_hook state but can't get the ringing state...OFF_HOOK state is called immediately when you press the calling button , and idle state is when you end the call .What I want is to know when other person's phone start ringing i.e. when connection is made and the person you are calling actually gets to know you are calling and can pick up your call – user2137186 Oct 21 '13 at 10:46
  • For me ringing state also coming – Pinki Oct 21 '13 at 12:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39648/discussion-between-android333-and-user2137186) – Pinki Oct 21 '13 at 12:31
1

There are 3 states: IDLE, RINGING, OFFHOOK.

For incoming calls the normal flow is:

  • IDLE (start state),
  • RINGING (the phone is ringing),
  • OFFHOOK (the user picks up)

For outgoing calls the flow is:

  • IDLE (start state),
  • OFFHOOK (called only after the other person picks up)

There's no RINGING state for outgoing calls before they're picked up.

marmor
  • 27,641
  • 11
  • 107
  • 150