How to make a phone call from your Android App?

How to make a phone call from your Android App?

To make a phone call from your Android app, you'll need to:

  1. Request the necessary permission.
  2. Use an Intent to initiate the call.

Here's how you can do it:

1. Add Permission:

Firstly, add the CALL_PHONE permission to your AndroidManifest.xml:

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

2. Implement the Call Logic:

In your activity or fragment, you can use the following code to initiate a call:

private void makePhoneCall(String phoneNumber) { if (phoneNumber.trim().length() > 0) { if (ContextCompat.checkSelfPermission(YourActivityName.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(YourActivityName.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL); } else { String dial = "tel:" + phoneNumber; startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial))); } } else { Toast.makeText(YourActivityName.this, "Enter Phone Number", Toast.LENGTH_SHORT).show(); } } 

Here, REQUEST_CALL is a constant (for example, private static final int REQUEST_CALL = 1;) that you can define in your activity. It's used to identify the permission request when the result is returned in the onRequestPermissionsResult method.

3. Handle Permission Result:

Override the onRequestPermissionsResult method to handle the permission result:

@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_CALL) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { makePhoneCall(phoneNumber); // Assuming phoneNumber is defined } else { Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show(); } } } 

4. Using the Code:

Now, you can make a call using the makePhoneCall method:

String phoneNumber = "1234567890"; makePhoneCall(phoneNumber); 

Replace 1234567890 with the actual phone number you wish to call.

Note:

Ensure you always check for the necessary permissions at runtime, especially if targeting Android 6.0 (API level 23) or higher. Calling startActivity for ACTION_CALL without the CALL_PHONE permission will result in a SecurityException. Always inform the user why you need the permission for better user experience and acceptance rate.

Examples

  1. Adding phone call functionality to your Android app:

    • To add phone call functionality, you'll need to request the CALL_PHONE permission in your AndroidManifest.xml and handle runtime permission if your app targets Android 6.0 (API level 23) or higher.
    <!-- AndroidManifest.xml --> <uses-permission android:name="android.permission.CALL_PHONE" /> 
  2. Making calls programmatically in Android app:

    • Use the following code snippet to make a phone call programmatically:
    // Inside your activity or fragment String phoneNumber = "tel:" + "123456789"; // Replace with the desired phone number Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri.parse(phoneNumber)); startActivity(dialIntent); 
  3. Phone call integration in Android app code sample:

    • Here's a complete code sample for integrating phone call functionality:
    // Inside your activity or fragment import android.content.Intent; import android.net.Uri; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Example: Make a phone call when a button is clicked findViewById(R.id.callButton).setOnClickListener(view -> makePhoneCall()); } private void makePhoneCall() { String phoneNumber = "tel:" + "123456789"; // Replace with the desired phone number Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri.parse(phoneNumber)); startActivity(dialIntent); } } 

    Make sure to replace "123456789" with the actual phone number you want to call and handle necessary permissions and runtime checks.


More Tags

mcrypt shader qt embedded-tomcat-8 delphi-2007 office365 reactive-streams redis-cli bufferedinputstream jquery

More Programming Guides

Other Guides

More Programming Examples