Retrieving Telephony Information

Developers can leverage the TelephonyManager class

(android.telephony.TelephonyManager) to access network status information about the device programmatically. You can get an instance of TelephonyManager by using the familiar getSystemService() method of the application's Context object:

TelephonyManager telMgr = (TelephonyManager)

getSystemService(Context. TELEPHONY_SERVICE);

Once you have a valid instance of TelephonyManager, you can request the detailed information about the device and its telephony services.

Retrieving Call State Information

You can use the TelephonyManager class to determine the call state of a cellular device by using the getCallState() method. This method tells you whether the phone is idle, off the hook, or currently receiving an incoming call.

You can also register to listen for changes in the phone state by using the listen() method, so that your application can receive a notification when an incoming call occurs. You might use this information to screen specific phone numbers or prepare the application for the user to answer the phone.

Retrieving Network Type Information

You can use the TelephonyManager class to query the network radio type of a cellular network by using the getNetworkType() method. This method tells you which network type the device is currently using, such as GPRS, EDGE, or EVDO. These and many other network types (for example, NETWORK_TYPE_GPRS) are defined as constants in the TelephonyManager class.

You can also register to listen for changes in the network type by using the listen() method. You might use this type of information to listen for a high-speed data connection and use the opportunity to download larger batches of quiz questions in the background to cache for future use in the game or to modify the batch size to download the optimum number of questions at a time to keep your application responsive to users and improve user experience.

Determining Whether the Phone Is CDMA or GSM

You can use the getPhoneType() method of the TelephonyManager class to determine whether a handset is a CDMA or GSM phone. Furthermore, you can get the unique identifier of the handset by using the getDeviceId() method. This method returns the IMEI for GSM phones or the MEID for CDMA phones. You can also get the unique subscriber identifier (such as IMSI for GSM subscribers) by using the getSubscriberId() method.

Retrieving SIM Information

The TelephonyManager class has a number of methods for retrieving information about the SIM card and SIM provider. The following are some examples:

► getSimState()—This method returns the state of the SIM card, such as whether the SIM card is in the handset and whether it is locked (that is, requires a PIN) or ready for use.

► getSimSerialNumber()—This method returns the unique serial number of the SIM card.

► getSimOperatorName()—This method returns the name of the SIM provider.

► getSimOperator()—This method returns the mobile country code and network code for the SIM provider.

Retrieving Device Voice Mail Information

You can use the TelephonyManager class to retrieve information about the phone user's voice mail. For example, you access the voice mail number by using the getVoiceMailNumber() method.

Retrieving Network Roaming Information

You've already seen how to access roaming information from the networking services, found in Hour 15, "Adding Network Support." You can also determine whether the subscriber service is roaming by using the isNetworkRoaming() method of the TelephonyManager class. This is not necessarily a reliable method for determining whether a user will be charged extra for data or phone calls but you can consider it a hint that the user might be charged (depending on the user's service plan). However, an application could use this information to prompt the user to choose whether to allow network usage when roaming, with a message such as "Your handset appears to be roaming. Would you like to continue?"

Retrieving Other Telephony Status Information

The TelephonyManager class also has many other methods for determining finegrained device telephony status information. See the Android SDK documentation for more details.

You can use the SmsManager class (android.telephony.SmsManager) and the SmsMessage class (android.telephony.SmsMessage) to send SMS messages.

Did you Know?

+1 0

Post a comment