Книга: Beginning Android

You Make the Call!

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_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 then 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 said 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 shown in Figure 35-1.


Figure 35-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 in Figure 35-2.


Figure 35-2. The Android Dialer activity, as launched from DialerDemo

Оглавление книги

Оглавление статьи/книги

Генерация: 1.386. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз