androidphone-call

How to make a phone call programmatically?


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?


Solution

  • 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" />