Выбрать главу

• 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. That class lets you do things like:

• 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, 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_DIAL Intent with a Uri of the form teclass="underline" 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 diaclass="underline" "

  />

  <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 = "teclass="underline" " + 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

CHAPTER 36

Searching with SearchManager

One of the firms behind the Open Handset Alliance — Google — has a teeny-weeny Web search service, one you might have heard of in passing. Given that, it’s not surprising that Android has some built-in search capabilities. Specifically, Android has baked in the notion of searching not only on the device for data, but over the air to Internet sources of data. Your applications can participate in the search process by triggering searches or perhaps by allowing your application’s data to be searched.

Note that this is fairly new to the Android platform, and so some shifting in the APIs is likely.

Hunting Season

There are two types of search in Android: local and global. Local search searches within the current application; global search searches the Web via Google’s search engine. You can initiate either type of search in a variety of ways, including the following:

• You can call onSearchRequested() from a button or menu choice, which will initiate a local search (unless you override this method in your activity).

• You can directly call startSearch() to initiate a local or global search, including optionally supplying a search string to use as a starting point.

• You can elect to have keyboard entry kick off a search via setDefaultKeyMode(), for either local search (setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL)) or global search (setDefaultKeyMode(DEFAULT_KEYS_SEARCH_GLOBAL)).

In either case, the search appears as a set of UI components across the top of the screen, with your activity blurred underneath it (see Figures 36-1 and 36-2).