Code-G
Code-G

Reputation: 41

How to make two calls in a row from an android app

Do any of you guys know how is it possible to make two calls in a row? Upon clicking a button the app should call the first number and after that it should sense the the first call have just ended and call automatically the second number. With the following code it just calls the first number.

private void callBack(String phone) {
    Intent callIntent = new Intent(Intent.ACTION_CALL)
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    callIntent.setData(Uri.parse("tel:" + phone));
    callIntent.putExtra("com.android.phone.extra.slot", 1);
    startActivity(callIntent);
}


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

    Button b = (Button) this.findViewById(R.id.CallButton);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          callBack("111111111");
          callBack("222222222");
       }});
}

Upvotes: 2

Views: 190

Answers (2)

Jay Patel
Jay Patel

Reputation: 317

Just Put Below class which is receive and handle Call broadcast

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

            Context mContext;
            String incoming_nr;
            private int prev_state;

            @Override
            public void onReceive(Context context, Intent intent) {
                TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
                CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
                telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager
                mContext = context;
            }
            /* Custom PhoneStateListener */
            public class CustomPhoneStateListener extends PhoneStateListener {

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

                    if (incomingNumber != null && incomingNumber.length() > 0) incoming_nr = incomingNumber;

                    switch (state) {
                        case TelephonyManager.CALL_STATE_RINGING:
                            prev_state = state;
                            break;
                        case TelephonyManager.CALL_STATE_OFFHOOK:
                            prev_state = state;
                            break;
                        case TelephonyManager.CALL_STATE_IDLE:
                            if ((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)) {
                                prev_state = state;
                                Toast.makeText(mContext, "Call End", Toast.LENGTH_SHORT).show();
                                //Answered Call which is ended  
                            }
                            if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) {
                                prev_state = state;
                                //Rejected or Missed call  
                            }
                            break;

                    }
                }
            }
        } 

put Below permission and register receiver in manifest for call phone and know state for that if end or not

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

         <receiver android:name=".PhoneStateBroadcastReceiver">
                    <intent-filter>
                        <action android:name="android.intent.action.PHONE_STATE"></action>
                    </intent-filter>
                </receiver>

Note: - put some condition for dial a second number other wise it should be enter in infinite loop. - You need to register/unregister broadcast receiver from acivitiy/fragment where you want to handle call end/restart second call action.

Upvotes: 1

Chris Gomez
Chris Gomez

Reputation: 6794

You could have a broadcastreceiver

public class EndCallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

     Bundle bundle = intent.getExtras();
     String phoneNumber= bundle.getString("incoming_number");   
     if (phoneNumber == "first_number") {
         //call second one
     }     
  }

}

You would have to register it on your manifest

<receiver android:name=".EndCallReceiver" >
   <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
   </intent-filter>
</receiver>

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

Upvotes: 1

Related Questions