0

I would like to make two calls in a row in an android app. Upon clicking button the app calls the first number. I created the broadcastreceiver below, that detects when the first call ends. It should write out that "First call ended" and then call the second number. It does not seem working. Can anybody spot the mistake in my code?

public class MainActivity extends AppCompatActivity {

public 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);
   Intent intent = new Intent(this, CallReciever.class);
   PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243 , intent, 0);
   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("+36309577686");

        }
    });
}

public class CallReciever 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
                        //it writes out the call end, but does not call. why?
                        Toast.makeText(mContext, "Call End", Toast.LENGTH_SHORT).show();
                        calling("+36303853440");
                        prev_state = state;
                    }
                    else if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) {
                        // Rejected or Missed call
                        Toast.makeText(mContext, "Rejected Call", Toast.LENGTH_SHORT).show();
                        prev_state = state;
                    }
                    break;

            }
        }
    }
}
}
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
Code-G
  • 41
  • 5

1 Answers1

0

You are not registering the CallReciever anywhere. Try this

Button b = (Button) this.findViewById(R.id.CallButton);


b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        calling("+36309577686");
        registerReceiver(new CallReceiver());
    }
});

Then unregister the receiver on broadcast is received

arjun
  • 3,514
  • 4
  • 27
  • 48
  • but a registered in the manifest with that: isn't that engough? (I also tried your suggeston, but than it requires an 'registerReceiver()' method) – Code-G Jan 26 '17 at 18:03
  • can u post you AndroidManifest.xml file. – arjun Jan 27 '17 at 05:26
  • you can have a look at it here: codeshare.io/GkmbjA – Code-G Jan 27 '17 at 12:37
  • the problem might be in permission if you are running the app in Android version 6.0 or more. Because by default the permission will be not granted, you have to request permission runtime. If your device android version is greater than 6.0, let me know I will give reference for getting permission at runtime – arjun Jan 27 '17 at 13:31
  • no, it is Android 4.4.4 But could you give me the referene? Maybe it works. – Code-G Jan 27 '17 at 13:42
  • in the code you have sent me, in the registerReceiver(), there should be also an intentfilter, right? (registerReceiver(SomeClass, IntentFilter)). Can it be a solution for the problem? What kind of intentfilter should I create? – Code-G Jan 27 '17 at 13:50
  • reference for runtime permission https://developer.android.com/training/permissions/requesting.html – arjun Jan 27 '17 at 16:13
  • refer this SO answer for call detection http://stackoverflow.com/a/15564021/2809326 – arjun Jan 27 '17 at 16:15
  • thanks. is there an SO answer also for outgoing calls? – Code-G Jan 27 '17 at 16:27