Android Dial Phone Programmatically
Have you imagined your app can make a phone call for you directly? Yes, Android phone can do this for you easily with only a few steps!
Android provides two ways to call a phone number from app — One is directing user to the keypad page of System “Phone” app and the another one is directly making a phone call for user. To provide a smoother user experience, the later method should be considered but developer should take care for the runtime permission (CALL_PHONE
) carefully.
In this article, I would go through the steps and details to implement this flow.
Telephone URI Standard
Before start coding, there is an IETF standard defines the URI specification for a phone call. A phone call URI contains four parts:
- Schema:
tel:
which defines this URI represents a telephone call - Preserved character: “+” which defines the start of the country code
- Country code: Length ranges from 1 to 4 number, e.g.
852
represents Hong Kong - Phone number: Self-explanatory
Below is an example of telephone URI for a phone call to Hong Kong (+852) with phone number “1878200”. The overall URI is tel:+8521878200
.
Method 1) Dial a number (Permission-free)
Step 1:
We have to parse the readable telephone string to Uri
object by the Uri.parse()
function.
Step 2:
Set the Intent Action
of Intent
to Intent.ACTION_DIAL
such that Android system can filter and delegate the dialling action to any app that can make a phone call.
Step 3:
Set the phone call URI to the Intent data in order to pass the phone number to the target phone call app.
Step 4:
Start an Activity
that can accept making a phone call.
Method 2) Make a phone call directly
The steps for making a phone call directly is similar to dialling a phone call except it requires a runtime permission CALL_PHONE
and a new Intent Action
Intent.ACTION_CALL
(See line 15 of the above code).
CALL_PHONE permission is a dangerous runtime permission since it can instantiate a phone call directly without going through the Dialler user interface for the user to confirm the call.
To request CALL_PHONE
permission, it is recommended to use the simple RxPermissions
library which takes care the Activity
lifecycle during requesting the permission. See line 6 to line 9 for the set up.
Summary
- App can help user to make a phone call directly or dial a phone number at System Phone app.
- Dialling a phone number is permission-free and easy to use. However, it is less user-friendly since it requires user to press the dial button to make a phone call.
- Making a phone call directly requires a
CALL_PHONE
runtime permission but can provide a better user experience. - Telephone URI standard is defined by the IETF and consists of 4 parts, i.e. schema, preserved plus symbol, country code and phone number.
You are welcome to follow me at Twitter@myrick_chow for more information. Thank you for reading this article. Have a nice day! 😄