Accessing the Dialer
November 19th, 2008
This is how easy it is to activate the Android dialer:
startActivity(new Intent(ACTION_DIAL, Uri.parse("tel:6365551212")));
And here is a complete Activity that shows the code in context. Specifically, this code:
- Creates a screen (an “Activity”) with a button
- Registers a listener with the button
- When you click the button, it starts the dialer
- When dialing completes, the user returns to this screen
public class DialerDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button dialButton = (Button) findViewById(R.id.dial_button);
dialButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialPhone();
}
});
}
private void dialPhone() {
Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri
.parse("tel:6365551212"));
startActivity(dialIntent);
}
}
When you run this you see a button:

Clicking the button brings up the dialer, with the number pre-typed:

Then you hit the green phone button to make the call:

Making the Call
You can also write code that actually makes the phone call using the ACTION_CALL intent. For that to work, your manifest needs to include the CALL_PHONE permission.
Perhaps you could use this feature to keep track of your kids. If they venture too many miles away from home, you could have their phone automatically dial home.
you low-level guys crack me up:
> This is how easy it is
followed by 30 lines of gibberish.
no, sir, this is what _easy_ looks like:
> dial 636-555-1212
1 line, crystal-clear. high-level rocks.
-bowerbird
@bowerbird:
While I would agree that Java could be a bit more concise, it’s not a fair argument to make that all of the above code could be condensed into one line.
The code above does more than just dial: It creates the view, sets up the button listener and executes the code that does the actual dialing.
Could it be reduced from 18 lines of Java to something more straightforward in another language?…sure. But one line is not realistic in any language.
@bowerbird I considered just showing this:
startActivity(new Intent(ACTION_DIAL, Uri.parse(”tel:6365551212″));
But then the example would not show any context, which isn’t all that helpful.
“How easy it is” is a phrase that can be used to demonstrate difficulty: “This is how easy it is to write a mobile platform operating system… (followed by 400k lines of source code)”.
What’s that ‘R’ business (R.id.dial_button)?
@Mario R is found in every Android program. It is generated from XML files. So you edit a String or layout in XML, then R.java is automatically updated. Then in your code, you get efficiency, compiler checks, and code completion because your code always deals with the generated constants in R.java.
It seems like dialing and maybe many other activities will be used commonly enough in Android applications that there should be convenience methods for them in the library.
For example, maybe Activity.dial(”6365551212″) should be a static method that does the following:
startActivity(new Intent(ACTION_DIAL, Uri.parse(”tel:6365551212″));
I don’t think shortening that line for some intents is a good idea, as it will create two ways to do things without providing much benefit (a little less verbosity). I think in such cases it’s better to get accustomed to the “generic” way of doing things, so that you don’t have to learn yet-another-idiom when you finally need to call something that isn’t built into Android.
@Ahnfelt I agree! Plus adding a bunch of redundant methods makes the Android class files larger, which is bad for mobile platforms.
I tried its fantastic its work cool, my project need a SMS to send from mobile , how can I call that through any fuction.
Is there any way we can call the SMS module like a call module , Please help budy
Just wanted to say thx for posting this. I’m currently taking a Mobile Programming course which is very intense and this code helped me understand intents better. I’m a novice(only 3 days programming for android) and this provided some great insight. Thx alot.