Blog Details

img
Design

What is Android Call State

Spoke Right / 19 Nov, 2023

The info about the telephony services, including the subscriber id, sim serial number, phone network type, phone state, etc., are facilitated by the android.telephony.TelephonyManager class. The TelephonyManager class of Android can also be used to get the information of the call state. The listen method of the TelephonyManager class is used to serve this purpose, by passing the PhonStateListener instance, which is necessary to implement to get the Call State, and facilitates a method onCallStateChanged().

Android Call State Example 1:

In the below example, we are demonstrating the usage of the Android TelephonyManager class to check if the phone is ringing or the phone is in a call or phone is neither ringing nor in a call.

Activity_main.xml:

In the activity_main.xml file, no new components will be added, and the code can remain as it is, except deleting the text.



 
    
 

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to know the call state.

package com.example.appexampleapp;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
 
 
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        TelephonyManager telephonyManager =
                (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 
        PhoneStateListener callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber)
            {
                if(state==TelephonyManager.CALL_STATE_RINGING){
                    Toast.makeText(getApplicationContext(),"Ringing",
                            Toast.LENGTH_LONG).show();
                }
                if(state==TelephonyManager.CALL_STATE_OFFHOOK){
                    Toast.makeText(getApplicationContext(),"Busy::Currently in another Call",
                            Toast.LENGTH_LONG).show();
                }
 
                if(state==TelephonyManager.CALL_STATE_IDLE){
                    Toast.makeText(getApplicationContext(),"Not Available:: Neither Ringing nor in a Call",
                            Toast.LENGTH_LONG).show();
                }
            }
        };
        telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
 
    }
 
 
}

AndroidManifest.xml:

In the AndroidManifest.xml file, we will add the code to provide the READ_PHONE_STATE permission.



 
    
 
    
    
 
    
        
            
                
 
                
            
        
 
 
    

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0