93
Android Call State Example
We can also get the information of call state using the TelephonyManager class. For this purpose, we need to call the listen method of TelephonyManager class by passing the PhonStateListener instance.
The PhoneStateListener interface must be implemented to get the call state. It provides one method onCallStateChanged().
Android Call State Example
Let’s see the example, where we are determining whether phone is ringing or phone is in a call or phone is neither ringing nor in a call.
activity_main.xml
In this example, we don’t have any component in this file..
Activity class
Let’s write the code to know the call state.
File: MainActivity.java
AndroidManifest.xml
You need to provide READ_PHONE_STATE permission in the AndroidManifest.xml file.
File: AndroidManifest.xml
Output: