— Android, Kotlin, Dialer App — 2 min read
Android devices come with a built-in dialer app that allows users to make phone calls directly. As an Android developer, you may encounter scenarios where you need to open the dialer app from your own application. This could be for various reasons, such as providing a shortcut for users or initiating calls with pre-filled numbers. In this article, we will discuss how to open the dialer app programmatically on Android using Kotlin.
The most common approach to open the dialer app programmatically is by using an Intent
. An Intent
is an abstract description of an operation to be performed. To open the dialer app, we can create an intent with the action ACTION_DIAL
and start it.
Here's an example of how to open the dialer app using an Intent
:
1val phoneNumber = "1234567890"2val intent = Intent(Intent.ACTION_DIAL)3intent.data = Uri.parse("tel:$phoneNumber")4startActivity(intent)
In the above code, we create a new Intent
with the action ACTION_DIAL
. We also set the data of the intent to a tel:
URI containing the phone number we want to pre-fill in the dialer app. Finally, we start the activity with the intent.
If you want to not only open the dialer app but also initiate a phone call automatically, you can use the ACTION_CALL
action. However, please note that this approach requires the CALL_PHONE
permission in your Android manifest.
Here's an example of how to open the dialer app and initiate a call using the ACTION_CALL
action:
1val phoneNumber = "1234567890"2val intent = Intent(Intent.ACTION_CALL)3intent.data = Uri.parse("tel:$phoneNumber")4startActivity(intent)
In the above code, we create an Intent
with the action ACTION_CALL
and set the data to a tel:
URI with the desired phone number. Then, we start the activity with the intent, which will open the dialer app and immediately initiate the call.
Please make sure to add the following permission to your Android manifest file:
1<uses-permission android:name="android.permission.CALL_PHONE" />
You'll also need to request this permission at runtime on devices running API 23(Android 6) or above.
Another way to open the dialer app is by using an implicit intent. In this approach, we don't specify the exact class name of the dialer app, allowing the system to find an appropriate application capable of handling the intent.
Here's an example of how to open the dialer app using an implicit intent:
1val phoneNumber = "1234567890"2val intent = Intent(Intent.ACTION_VIEW, Uri.parse("tel:$phoneNumber"))3if (intent.resolveActivity(packageManager) != null) {4 startActivity(intent)5}
In the above code, we create an Intent
with the action ACTION_VIEW
and set the data to a tel:
URI with the phone number. We then check if there's any activity available on the device that can handle this intent before starting it.
Opening the dialer app programmatically on Android can be achieved using different approaches such as Intent
with ACTION_DIAL
, ACTION_CALL
(requires permission), or an implicit intent. Depending on your use case, you can choose the appropriate approach to integrate this functionality into your application. Always remember to handle permissions and consider user privacy when initiating phone calls automatically.