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
. 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_DIAL
Intent
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 press 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"?>
<LinearLayout xmlns: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:"
/>
<EditText android: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!"
/>
</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:
package com.commonsware.android.dialer;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class DialerDemo extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final EditText number=(EditText)findViewById(R.id.number);
Button dial=(Button)findViewById(R.id.dial);
dial.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
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 you can see in Figure 34–1.
Figure 34–1. The DialerDemo sample application, as initially launched
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 34–2.
Figure 34–2. The Android Dialer activity, as launched from DialerDemo