Chapter 41

Handling Telephone Calls

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?

  • Maybe you are writing an Android interface to a sales management application (a la Salesforce.com) and you want to offer users the ability to call prospects with a single button tap, and without them having to keep those contacts both in your application and in the phone’s contacts application.
  • Maybe you are writing a social networking application, and the roster of phone numbers that you can access shifts constantly, so rather than try to sync the social network contacts with the phone’s contact database, you want to let people place calls directly from your application.
  • Maybe you are creating an alternative interface to the existing contacts system, perhaps for users with reduced motor control (e.g., the elderly), sporting big buttons and the like to make it easier for them to place calls.

Whatever the reason, Android has the means to let you manipulate the phone just like any other piece of the Android system.

Report to the Manager

To get at much of the phone API, you use the TelephonyManager class. That class lets you do things like the following:

  • Determine if the phone is in use via 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)
  • Find out the SIM ID (IMSI) via getSubscriberId()
  • Find out the phone type (e.g., GSM) via getPhoneType() or find out the data connection type (e.g., GPRS or EDGE) via getNetworkType()

You Make the Call!

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.

images

Figure 41–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 41–2.

images

Figure 41–2. The Android Dialer activity, as launched from DialerDemo

No, Really, You Make the Call!

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).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.2.23