Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
To make a phone call from your Android app, you'll need to:
Intent
to initiate the call.Here's how you can do it:
Firstly, add the CALL_PHONE
permission to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.CALL_PHONE" />
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.
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(); } } }
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.
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.
Adding phone call functionality to your Android app:
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" />
Making calls programmatically in Android app:
// 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);
Phone call integration in Android app code sample:
// 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.