knvchowdary
knvchowdary

Reputation: 81

making phone call from DialogBox in Android

I have one DialogBox with 2 buttons. If we click the +ve button, it attempts to open the Dialing Pad; else if we click the -ve button it closes the dialog. When I click the +ve button it shows the null exception. If the same code executes without the DialogBox, it is fine.

Here is my code:

callDialog.setPositiveButton("Call Now", new android.DialogInterface.
                OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent dial = new Intent();
                dial.setAction("android.intent.action.DIAL");
                try {
                    dial.setData(Uri.parse("tel:9951037343"));
                    startActivity(dial);
                } catch (Exception e) {
                    Log.e("Calling", "" + e.getMessage());
                }


            }

        }

I've given permissions in the manifest file as <uses-permission android:name="android.permission.CALL_PHONE"/>

Upvotes: 2

Views: 2252

Answers (6)

knvchowdary
knvchowdary

Reputation: 81

Actually I called the AlertDialog from another Activity,so I need the context of called Activity's Context reference.So I passed the Context as a parameter to Called Activity Here is Called Activity's code

if(position == 1)new CalledByActivity().dailingDialogBox(param1, param2,context);//Here I'm passing the context as param

Here CalledBy activity's implemented function

public void dailingDialogBox(final String param1,String param2,final Context context){

Here I implemented in +ve button click

Intent dail = new Intent(Intent.ACTION_DIAL);
dail.setData(Uri.parse("tel:"+station_Number));
context.startActivity(dail);//Here the "context" is reference of called Activity's context

Upvotes: 0

Sebastian
Sebastian

Reputation: 2718

Remove the @Overrride line. Like this:

callDialog.setPositiveButton("Call Now", new android.DialogInterface.
            OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent dial = new Intent();
            dial.setAction("android.intent.action.DIAL");
            try {
                dial.setData(Uri.parse("tel:9951037343"));
                startActivity(dial);
            } catch (Exception e) {
                Log.e("Calling", "" + e.getMessage());
            }


        }

    }

----UPDATE-----

Since you havent posted any logcat, its hard to know where it crashes. Try this block:

AlertDialog.Builder callDialog = new AlertDialog.Builder(this);
    callDialog.setTitle("My title");
    callDialog.setMessage("My message");
    callDialog.setPositiveButton("Call", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

            Intent dial = new Intent();
            dial.setAction("android.intent.action.DIAL");
            try {
                dial.setData(Uri.parse("tel:9951037343"));
                startActivity(dial);
            } catch (Exception e) {
                Log.e("Calling", "" + e.getMessage());
            }

        }
    });
    callDialog.setNegativeButton("Cancel", null);
    callDialog.show();

Upvotes: 1

Aritra
Aritra

Reputation: 436

    use this code......

    String number = "tel:" + phonenumber;
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri
                            .parse(number));
                    startActivity(intent);

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


updated.....



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.menu_icon_pager).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                new AlertDialog.Builder(MainActivity.this)

                .setIcon(android.R.drawable.ic_dialog_alert)

                .setTitle("Exit")

                .setMessage(
                        "Texting from dialog")

                .setNegativeButton(android.R.string.cancel, null)

                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {

                                // Exit the activity


                                String number = "tel:"+phonenumber ;
                                Intent intent = new Intent(Intent.ACTION_CALL, Uri
                                        .parse(number));
                                startActivity(intent);

                            }

                        })

                .show();




            }
        });


    }

Upvotes: 0

malavika
malavika

Reputation: 1331

Use

Intent callIntent = new Intent(Intent.ACTION_CALL);

instead of this

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setAction("android.intent.action.DIAL"); // but this line is an optional one
callIntent.setData(Uri.parse("tel:044 6000 6000"));
startActivity(callIntent);

Upvotes: 0

sjain
sjain

Reputation: 23344

Try the following---

Intent dial = new Intent(Intent.ACTION_DIAL); 
//dial.setAction("android.intent.action.DIAL");
                try {
                    dial.setData(Uri.parse("tel:9951037343"));
                    startActivity(dial);
                } catch (Exception e) {
                    Log.e("Calling", "" + e.getMessage());
                }
startActivity(dial); 

Upvotes: 0

Lal
Lal

Reputation: 14810

Try this..

Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:phonenumber");
startActivity(call);

Upvotes: 3

Related Questions