How to make a call via dialer programmatically in android Pie and above

79 Views Asked by At

I use this code to show the number and when pressing it to make a dial.It is working for android versions before Android Pie.

    final Button but= findViewById(R.id.buttond);
    but.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String PhNumber = "6998474783";////example number
            final CharSequence[] phones = PhNumber.split(" - ");
            AlertDialog.Builder builder = new AlertDialog.Builder(CTX);
            builder.setTitle("Επιλογή Τηλεφώνου");
            builder.setItems(phones, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    // Do something with the selection
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phones[item].toString()));
                    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {                           
                        return;                        }
                    startActivity(intent);                       
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

What I have to change to work with Pie and above? It shows the phone number but when I press it nothing happens

1

There are 1 best solutions below

0
On

Solved using ACTION_DIAL without any checkSelfPermission.

  Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phones[item].toString()));
    startActivity(intent); 

If is there any other way, maybe with telecom-Manager please post it.