Thứ Hai, 15 tháng 10, 2012
Android Telephony and SMS
12
Telephony and SMS
WHAT’S IN THIS CHAPTER?
➤ Initiating phone calls
➤ Reading the phone, network, data connectivity, and SIM states
➤ Monitoring changes to the phone, network, data connectivity, and
SIM states
➤ Using Intents to send SMS and MMS messages
➤ Using the SMS Manager to send SMS Messages
➤ Handling incoming SMS messages
In this chapter, you’ll learn to use Android’s telephony APIs to monitor mobile voice and data
connections as well as incoming and outgoing calls, and to send and receive SMS (short messag-ing service) messages.
You’ll take a look at the communication hardware by examining the telephony package for
monitoring phone state and phone calls, as well as initiating calls and monitoring incoming call
details.
Android also offers full access to SMS functionality, letting you send and receive SMS messages
from within your applications. Using the Android APIs, you can create your own SMS client
application to replace the native clients available as part of the software stack. Alternatively,
you can incorporate the messaging functionality within your own applications to create social
applications using SMS as the transport layer.
At the end of this chapter, you’ll use the SMS Manager in a detailed project that involves creat-ing an emergency SMS responder. In emergency situations, the responder will let users quickly,
or automatically, respond to people asking after their safety.
TELEPHONY
The Android telephony APIs let your applications access the underlying telephone hardware stack,
making it possible to create your own dialer — or integrate call handling and phone state monitoring
into your applications.
Because of security concerns, the current Android SDK does not allow you to
create your own ‘‘in call’’ Activity — the screen that is displayed when an incoming
call is received or an outgoing call has been placed.
The following sections focus on how to monitor and control phone, service, and cell events in your
applications to augment and manage the native phone-handling functionality. If you wish, you can use
the same techniques to implement a replacement dialer application.
Launching the Dialer to Initiate Phone Calls
Best practice is to use Intents to launch a dialer application to initiate new phone calls. Use an Intent
action to start a dialer activity; you should specify the number to dial using the tel: schema as the data
component of the Intent.
Use theIntent.ACTION_DIAL Activity action to launch a dialer rather than dial the number immediately.
This action starts a dialer Activity, passing in the specified number but allowing the dialer application
to manage the call initialization (the default dialer asks the user to explicitly initiate the call). This
action doesn’t require any permissions and is the standard way applications should initiate calls.
Listing 12-1 shows the basic technique for dialing a number.
LISTING 12-1:Dialing a number
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:1234567"));
startActivity(intent);
By using an Intent to announce your intention to dial a number, your application can remain decoupled
from the dialer implementation used to initiate the call. For example, if you were to replace the existing
dialer with a hybrid that allows IP-based telephony, using Intents to dial a number from your other
applications would let you leverage this new dialer functionality.
Replacing the Native Dialer
Replacing the native dialer application involves two steps:
1. Intercepting Intents that are currently serviced by the native dialer.
2. Initiating, and optionally managing, outgoing calls.
The native dialer application currently responds to Intent actions corresponding to a user’s pressing the
hardware call button, asking to view data using thetel: schema, or making a request to dial a number
using the tel: schema.
To intercept these requests include<intent-filter>tags on your new Activity that listens for the
following actions:
➤ Intent.ACTION_CALL_BUTTON This action is broadcast when the device’s hardware call but-
ton is pressed. Create an Intent Filter listening for this action as a default action.
➤ Intent.ACTION_DIAL The Intent action described in the previous section, this Intent is used
by applications which want to launch the dialer to make a phone call. The Intent Filter used
to capture this action should be both default and browsable (to support dial requests from the
browser), and must specify thetel: schema to replace existing dialer functionality (though it
can support additional schemes).
➤ Intent.ACTION_VIEW The view action is used by applications wanting to view a piece of
data. Ensure that the Intent Filter specifies the tel: schema to allow your new Activity to be
used to view telephone numbers.
The following manifest snippet shows an Activity with Intent Filters that will capture each of these
actions.
<activity
android:name=".MyDialerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.CALL_BUTTON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="tel" />
</intent-filter>
</activity>
Đăng ký:
Đăng Nhận xét (Atom)
Không có nhận xét nào:
Đăng nhận xét