简体   繁体   中英

How to give Choice of Numbers to Call using Intent.ACTION_CALL?

I am trying to give the option of two numbers on a click of a button to make a call on the number directly but when I am selecting one of the numbers it is going to the dialer screen but "Ljava.lang.CharSequence" is written there and a dialog box with a message service is not supported pops up.

Here is my code:-

phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                final CharSequence numbers[] = new CharSequence[] {"02212345678","+14356789809"};

                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("Select number to call");
                builder.setItems(numbers, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        String call = "tel:" +numbers;
                        callIntent.setData(Uri.parse(call));
                        startActivity(callIntent);


                    }
                });
                builder.show();


            }
        });

Change

 String call = "tel:" +numbers;

To

String call = "tel:" +numbers[which];
phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             String number = "02212345678";

                          AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("Select number to call");
                builder.setItems(number , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        String call = "tel:" +number ;
                        callIntent.setData(Uri.parse(call));
                        startActivity(callIntent);


                    }
                });
                builder.show();


            }
        });

This is the complete code for giving choice of numbers and calling the selected number on a Button Click. I hope this will help other members too.

phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                final CharSequence numbers[] = new CharSequence[] {"+1xxxxxxxxx","+1xxxxxxxxxx"};

                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("Select number to call");
                builder.setItems(numbers, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        String call = "tel:" +numbers[which];
                        callIntent.setData(Uri.parse(call));
                        startActivity(callIntent);

                        // the user clicked on colors[which]
                    }
                });
                builder.show();


            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM