Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Dialing the Phone

Author: Ian Darwin -- Published? true -- FormatLanguage: W

Problem:

You want to dial the phone from within an application, without worrying about details of telephony.

Solution:

Start an Intent to dial the phone.

Discussion:

One of the beauties of Android is the ease with which applications can re-use other applications, without being tightly coupled to the details (or even name) of the other program, using the Intent mechanism. For example, to call the phone, you only need to create and start an Intent with the action of DIAL and the "URI" of "tel" + the number you want to dial. Thus, a basic dialer can be as simple as this:

public class Main extends Activity {
	String phoneNumber = "555-1212";
	String intentStr = "tel:" + phoneNumber;
	
    /** Standard creational callback.
     * Just dial the phone
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Intent intent = new Intent("android.intent.action.DIAL", 
        		Uri.parse(intentStr));
        
        startActivity(intent);
    }
}

You need to have the permission android.permission.CALL_PHONE to use this code. The user will see this screen, and users know to press the green Phone button to let the call procede.

Typically in real life you would not hard-code the number. In other circumstances you might want the user to call a number from the phone's Contacts list.

No records found.