Many, if not most, Android devices will be phones. As such, not only will users be expecting to place and receive calls using Android, but you will have the opportunity to help them place calls, if you wish.
Why might you want to?
Whatever the reason, Android has the means to let you manipulate the phone just like any other piece of the Android system.
To get at much of the phone API, you use the TelephonyManager
class. That class lets you do things like the following:
getCallState()
, with return values of CALL_STATE_IDLE
(phone not in use), CALL_STATE_RINGING
(call requested but still being connected), and CALL_STATE_OFFHOOK
(call in progress)getSubscriberId()
getPhoneType()
or find out the data connection type (e.g., GPRS or EDGE) via getNetworkType()
You can also initiate a call from your application, such as from a phone number you obtained through your own web service. To do this, simply craft an ACTION_DIALIntent
with a Uri
of the form tel:NNNNN
(where NNNNN
is the phone number to dial) and use that Intent
with startActivity()
. This will not actually dial the phone; rather, it activates the dialer activity, from which the user can then tap a button to place the call.
For example, let’s look at the Phone/Dialer
sample application. Here’s the crude but effective layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number to dial:"
/>
<EditTextandroid:id="@+id/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
/>
</LinearLayout>
<Button android:id="@+id/dial"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Dial It!"
android:onClick="dial"
/>
</LinearLayout>
We have a labeled field for typing in a phone number, plus a button for dialing that number.
The Java code simply launches the dialer using the phone number from the field:
packagecom.commonsware.android.dialer;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.EditText;
public class DialerDemo extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
public void dial(View v) {
EditText number=(EditText)findViewById(R.id.number);
String toDial="tel:"+number.getText().toString();
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(toDial)));
}
}
The activity's own UI is not that impressive, as shown in Figure 41–1.
However, the dialer you get from clicking the dial button is better, showing you the number you are about to dial, as shown in Figure 41–2.
The good news is that ACTION_DIAL
works without any special permissions. The bad news is that it takes the user only to the dialer. The user still has to take action (press the green call button) to actually place the phone call.
An alternative approach is to use ACTION_CALL
instead of ACTION_DIAL
. Calling startActivity()
on an ACTION_CALLIntent
will immediately place the phone call, without any other UI steps required. However, you need the CALL_PHONE
permission to use ACTION_CALL
(see Chapter 38).