0

So I try to make a app that will test if a phone can call phone numbers. And the main point is that when i push a call button and when the connection begins app ends a call and if there is no connection app shows an error. I managed to do a part where when button is clicked app calls a phone number but I can't come up with solution for ending a cal when there is connection (ringing). Is there any way to make this possible?

Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                String phone = "8686868686";
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.fromParts("tel", phone, null));
                startActivity(intent);

                if (ActivityCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }

                /*Context mContext = getApplicationContext();
                TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);

                if (tm != null) {
                    boolean success = tm.endCall();
                    // success == true if call was terminated.
                }*/
            }
        });
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Robertas
  • 59
  • 1
  • 4
  • You could have a look at [PhoneStateListener](https://developer.android.com/reference/android/telephony/PhoneStateListener). – Markus Kauppinen Jul 30 '19 at 07:01
  • check this I think will help you : https://stackoverflow.com/questions/28793946/how-to-cancel-outgoing-call-in-android-dev – Mina George Jul 30 '19 at 07:31
  • Still havent found an answer... :( – Robertas Jul 30 '19 at 10:33
  • I think [PhoneStateListener.onCallStateChanged()](https://stackoverflow.com/questions/5741319/phone-call-state) gives you `CALL_STATE_OFFHOOK` once the call is being dialled. But ending the call [might be either difficult or impossible](https://stackoverflow.com/questions/18065144/end-call-in-android-programmatically) depending on the Android version. Your app migh become obsolete once newer Android versions become more common as it looks like you can't end the call programmatically in the latest versions. – Markus Kauppinen Jul 30 '19 at 11:27

0 Answers0