• 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 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:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number to dial:"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Dial It!"
/>
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:
packagecom.commonsware.android.dialer;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
public classDialerDemo extendsActivity {
@Override
publicvoid onCreate(Bundle icicle) {
super. onCreate(icicle);
setContentView(R.layout.main);
finalEditText number = (EditText) findViewById(R.id.number);
Button dial = (Button) findViewById(R.id.dial);
dial. setOnClickListener( newButton. OnClickListener() {
publicvoid 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
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.
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).
Figure 36-1. The Android local search pop-up
Figure 36-2. The Android global search pop-up, showing a drop-down with previous searches
Over the long haul, there will be two flavors of search available via the Android search system:
• Query-style search, where the user’s search string is passed to an activity that is responsible for conducting the search and displaying the results
• Filter-style search, where the user’s search string is passed to an activity on every key press, and the activity is responsible for updating a displayed list of matches
Since the latter approach is under heavy development right now by the Android team, let’s focus on the first one.
Craft the Search Activity
The first thing you’ll want to do if you want to support query-style search in your application is to create a search activity. While it might be possible to have a single activity be both opened from the launcher and opened from a search, that might prove somewhat confusing to users. Certainly, for the purposes of learning the techniques, having a separate activity is cleaner.
Читать дальше