-1

I would like to make make two calls in a row in an android app. Upon clicking a button the app should call the first number and after that it should sense the the first call have just ended (should not matter which party hook up) and call automatically the second number. I've learned that it is possible to detect that a call ended with the class that I placed below the class MainActivity. (class PhoneStateBroadcastReceiver ). Still I do not really understand what should I know write into my main class. I guess, I should write something between the two lines where I call the calling method to get the state of the phone, right?

package com.example.bedaa.drivecaller;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;

 public class MainActivity extends AppCompatActivity {



private void calling(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) {
          calling("11111111");
          calling("22222222");


        }
      });

   }
 }

 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;

        }
    }
}

}

Code-G
  • 41
  • 5
  • Possible duplicate of [What is BroadcastReceiver and when we use it?](http://stackoverflow.com/questions/5296987/what-is-broadcastreceiver-and-when-we-use-it) – Michael Dodd Jan 25 '17 at 14:15
  • @MichaelDodd, thanks for you fast answer, i found your link quite useful :) but in that Question Bytecode asks "anybody please tell me the concept of BroadcastReceiver. Its use and how to use it ". I think my case is a bit more specific. – Code-G Jan 25 '17 at 14:24

1 Answers1

0

On the button click, you should call the first number:

public void onClick(View v) {
      calling("+11111111111");
}

Then you should wait for the result from your BroadcastReceiver before calling the second number:

class PhoneStateBroadcastReceiver extends BroadcastReceiver {

    private Context mContext;
    private CustomPhoneStateListener mPhoneListener;
    private String incoming_nr;
    private int prev_state;

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

        if (mPhoneListener == null) {
            mPhoneListener = new CustomPhoneStateListener();

            // TelephonyManager object
            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);


            // Register our listener with TelephonyManager
            telephony.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    /* Custom PhoneStateListener */
        class CustomPhoneStateListener extends PhoneStateListener {

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

            if (!TextUtils.isEmpty(incomingNumber)) {
                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)) {
                        // A call has now ended
                        Toast.makeText(mContext, "Call End", Toast.LENGTH_SHORT).show();
                        calling("+22222222222");
                        prev_state = state;
                    }
                    else if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) {
                        // Rejected or Missed call
                        prev_state = state;
                    }
                    break;

            }
        }
    }
}

============================================

Make sure to include in your manifest:

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

And maybe change your "calling" method to the following:

private void calling(String number) {
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
    startActivity(intent);
}
Charlie
  • 2,876
  • 19
  • 26
  • hi @Charlie, thanks for you answer. should it also work if I hook up the phone while it is still ringing? – Code-G Jan 25 '17 at 15:08
  • but it still does not call the second number in any cases. do you have any idea what shoudl be a the problem? – Code-G Jan 25 '17 at 15:14
  • In the example above it would only call the second number if the first call was successful (e.g. the user answered) – Charlie Jan 25 '17 at 15:26
  • i have tried it that way, but still did not work. i think I put somewhere a mistake – Code-G Jan 25 '17 at 15:41
  • I've made an edit to my answer. Make sure to declare the permission in your manifest, and I've also suggested an edit for your "calling" method :) – Charlie Jan 25 '17 at 15:50
  • I really appreciate your helpfulness :) could you have a look on my whole code (codeshare.io/GqPQxM), because something still does not seem to work. – Code-G Jan 25 '17 at 16:22