Android Get Call State

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

  1. package com.javatpoint.callstates;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.telephony.PhoneStateListener;  
  7. import android.telephony.TelephonyManager;  
  8. import android.view.Menu;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.           
  18.         TelephonyManager telephonyManager =  
  19.                       (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);  
  20.          
  21.         PhoneStateListener callStateListener = new PhoneStateListener() {  
  22.         public void onCallStateChanged(int state, String incomingNumber)   
  23.         {  
  24.               if(state==TelephonyManager.CALL_STATE_RINGING){  
  25.                         Toast.makeText(getApplicationContext(),"Phone Is Riging",  
  26.                                                                          Toast.LENGTH_LONG).show();  
  27.               }  
  28.                 if(state==TelephonyManager.CALL_STATE_OFFHOOK){  
  29.                     Toast.makeText(getApplicationContext(),"Phone is Currently in A call",   
  30.                                                                                   Toast.LENGTH_LONG).show();  
  31.                 }  
  32.                                   
  33.                 if(state==TelephonyManager.CALL_STATE_IDLE){  
  34.                     Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call",  
  35.                                                                                                  Toast.LENGTH_LONG).show();  
  36.                 }  
  37.         }  
  38.         };  
  39.         telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);  
  40.           
  41.     }  
  42.   
  43.     @Override  
  44.     public boolean onCreateOptionsMenu(Menu menu) {  
  45.         // Inflate the menu; this adds items to the action bar if it is present.  
  46.         getMenuInflater().inflate(R.menu.main, menu);  
  47.         return true;  
  48.     }  
  49.       
  50. }  

AndroidManifest.xml

You need to provide READ_PHONE_STATE permission in the AndroidManifest.xml file.

File: AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:androclass="http://schemas.android.com/apk/res/android"  
  3.     package="com.javatpoint.callstates"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.    
  11.        
  12.        <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  13.          
  14.     <application  
  15.         android:allowBackup="true"  
  16.         android:icon="@drawable/ic_launcher"  
  17.         android:label="@string/app_name"  
  18.         android:theme="@style/AppTheme" >  
  19.         <activity  
  20.             android:name="com.javatpoint.callstates.MainActivity"  
  21.             android:label="@string/app_name" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.     </application>  
  29.   
  30. </manifest>  



Output:

android call state example output 1 android call state example output 2