Tuesday, February 14, 2017

Phone callbacks in Android Using TelephonyManager

If you are an Android Developer and you need to detect if your phone is in idle state, off-hook (receiving state) or ringing state, this tutorial was created for you. The objective here is to show you how to implement phone callbacks in Android, allowing detection of calls to your phone.

In this topic, the most important class that will be talked about is the TelephonyManager. This class uses a listener called PhoneStateListener to listen for telephony service updates.

An object of type TelephonyManager will be instantiated, and it will listen for updates of Context.TELEPHONY_SERVICE. In order to monitor updates of telephony states like PhoneStateListener.LISTEN_DATA_CONNECTION_STATE, PhoneStateListener.LISTEN_CELL_INFO and others, a class called PhoneCallback will be created, extending from PhoneStateListener.

With TelephonyManager the telephony services of a device can be accessed and with the method TelephonyManager.listen(PhoneStateListener listener, int events) all information the telephone is receiving can be monitored.

The class PhoneStateListener receives callbacks when some event of TELEPHONY_SERVICE is observed. In this tutorial, the flag PhoneStateListener.LISTEN_CALL_STATE will be used. It will monitor and listen to the call state of the phone.

As can be read in the PhoneStateListener documentation (see section “Further Reading” below), there are other constants that can be set into the argument int events of the method TelephonyManager.listen. For instance: LISTEN_SIGNAL_STRENGTHS listens for changes to the network signal strengths (cellular). But in this tutorial, the flag LISTEN_CALL_STATE will be used, which listens for changes to the device call state.

Continue reading %Phone callbacks in Android Using TelephonyManager%


by Rodrigo Cericatto via SitePoint

No comments:

Post a Comment