How to make a phone call programmatically?

AndroidPhone Call

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

Tried this on my phone and it works perfectly.

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:900..." ));
startActivity(intent);

Add this permission in manifest file.

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

Solution 3 - Android

 Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+198+","+1+","+1)); 
			 startActivity(callIntent);

for multiple ordered call

This is used to DTMF calling systems. If call is drop then, you should pass more " , " between numbers.

Solution 4 - Android

Take a look there : http://developer.android.com/guide/topics/intents/intents-filters.html

DO you have update your manifest file in order to give call rights ?

Solution 5 - Android

This doesn't require a permission.

val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:+123456"))
startActivity(intent)

Or

val intent = Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", "+123456", null))
startActivity(intent)

But it shows one more dialog (asking whether you want to call phone just once or always). So it would be better to use ACTION_CALL with a permission (see https://stackoverflow.com/questions/33473436/revoked-permission-android-permission-call-phone).

Solution 6 - Android

Here I will show you that how you can make a phone call from your activity. To make a call you have to put down this code in your app.

try {
    Intent my_callIntent = new Intent(Intent.ACTION_CALL);
    my_callIntent.setData(Uri.parse("tel:"+phn_no));
    //here the word 'tel' is important for making a call...
    startActivity(my_callIntent);
} catch (ActivityNotFoundException e) {
    Toast.makeText(getApplicationContext(), "Error in your phone call"+e.getMessage(), Toast.LENGTH_LONG).show();
}

Solution 7 - Android

It is working very well. In this way, you do not need to have permission from user. You can open directly phone calling part. > Trick point, use ACTION_DIAL instead of ACTION_CALL.

private void callPhoneNumber() {
    String phone = "03131693169";
    Intent callIntent = new Intent(Intent.ACTION_DIAL);
    callIntent.setData(Uri.parse("tel:" + phone));
    startActivity(callIntent);
}

Solution 8 - Android

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main); 
   final Button button = (Button) findViewById(R.id.btn_call);
	button.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			String mobileNo = "123456789";
			String uri = "tel:" + mobileNo.trim();
			Intent intent = new Intent(Intent.ACTION_CALL);
			intent.setData(Uri.parse(uri));
			startActivity(intent);
		}
	});*
 }

Solution 9 - Android

If anyone is looking for in Kotlin

    val  uri = "tel:+800******"
    val call_customer_service = Intent(Intent.ACTION_CALL)
    call_customer_service.setData(Uri.parse(uri))
    startActivity(call_customer_service)

Like some other solutions it requires android.permission.CALL_PHONE permission.

Solution 10 - Android

If you end up with a SecurityException (and the call does not work), You should consider requesting the user permission to make a call as this is considered a dangerous permission:

ActivityCompat.requestPermissions(
    activity,
    new String[] {Manifest.permission.CALL_PHONE},
    1
);

Note this has nothing to do with the manifest permission (that you must have as well)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNullPointerExceptionView Question on Stackoverflow
Solution 1 - AndroidLiorView Answer on Stackoverflow
Solution 2 - AndroidAnirudhView Answer on Stackoverflow
Solution 3 - AndroidAshish DwivediView Answer on Stackoverflow
Solution 4 - AndroidykatchouView Answer on Stackoverflow
Solution 5 - AndroidCoolMindView Answer on Stackoverflow
Solution 6 - AndroidPir Fahim ShahView Answer on Stackoverflow
Solution 7 - AndroidNamelessView Answer on Stackoverflow
Solution 8 - AndroidMuhammad Usman GhaniView Answer on Stackoverflow
Solution 9 - AndroidMughilView Answer on Stackoverflow
Solution 10 - AndroidLi3roView Answer on Stackoverflow