How to make a phone call programmatically?

NullPointerException picture NullPointerException · Jan 27, 2011 · Viewed 127.6k times · Source

I'm passing to an activity the number to call by a bundle

and then, in such activity, I have a button to call to that number, this is the code:

callButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(bundle.getString("mobilePhone")));
            }
        }); 

Something is wrong, because when I press the button nothing happens...

What am I doing wrong?

PD: I'm using Android 1.5 compatible project... maybe phone call is incompatible to 1.5?

Answer

Lior picture Lior · Jan 27, 2011

You forgot to call startActivity. It should look like this:

Intent intent = new Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));
context.startActivity(intent);

An intent by itself is simply an object that describes something. It doesn't do anything.

Don't forget to add the relevant permission to your manifest:

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