Android call state Listener / Telephony Manager Example


           Listening call state in Android is quite simple through Android Telephony manager. Let’s see an example with explanation below.

Telephony Manager
Android Telephony Manager provides access to information about the telephony services in android. This class provides various methods to access the telephony states and services. Now let’s see how to use telephony manager to know the incoming call states. Applications need to register a listener to receive notification on call states.
Permission in Android Manifest              
             Access to android telephony manager is permission – protected.  To access the states, we need to declare the permissions in manifest file.

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Example Code
              Now let me explain the code. Telephony manager class needs to be instantiated through getSystemService(Context.TELEPHONY_SERVICE).  Context is an interface to global information about the application. It allows access to application – specific resources and classes. getSystemService returns handle to system level service, by which we get access for handling telephony features of the device 
(TELEPHONY_SERVICE).

TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

Next, we need to register a listener to receive notification about the changes in telephony states.

TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);

If any change is detected in the call state the call back function - public void onCallStateChanged(int state, String incomingNumber) is triggered, with the state and incoming number.

Various states are - CALL_STATE_IDLE, CALL_STATE_OFFHOOK, and CALL_STATE_RINGING
IDLE             – when there is no incoming call
OFFHOOK   – when the line is busy
RINGING      – when call is incoming

We can write our own code in the blocks to handle Android call states.

switch (state){
    case TelephonyManager.CALL_STATE_IDLE:                      
       //CALL_STATE_IDLE;
       Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE", Toast.LENGTH_LONG).show();
       break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
       //CALL_STATE_OFFHOOK;
       Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK", Toast.LENGTH_LONG).show();
       break;
    case TelephonyManager.CALL_STATE_RINGING:
       //CALL_STATE_RINGING
       Toast.makeText(getApplicationContext(), incomingNumber, Toast.LENGTH_LONG).show();   
       Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING", Toast.LENGTH_LONG).show();
        break;
   default:
        break;
}




MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  TelephonyMgr.listen(new TeleListener(),
    PhoneStateListener.LISTEN_CALL_STATE);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 class TeleListener extends PhoneStateListener {
  public void onCallStateChanged(int state, String incomingNumber) {
   super.onCallStateChanged(state, incomingNumber);
   switch (state) {
   case TelephonyManager.CALL_STATE_IDLE:
    // CALL_STATE_IDLE;
    Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
      Toast.LENGTH_LONG).show();
    break;
   case TelephonyManager.CALL_STATE_OFFHOOK:
    // CALL_STATE_OFFHOOK;
    Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
      Toast.LENGTH_LONG).show();
    break;
   case TelephonyManager.CALL_STATE_RINGING:
    // CALL_STATE_RINGING
    Toast.makeText(getApplicationContext(), incomingNumber,
      Toast.LENGTH_LONG).show();
    Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
      Toast.LENGTH_LONG).show();
    break;
   default:
    break;
   }
  }

 }
}

Source Code of this Application




Reactions:

7 comments :

  1. i got Sintax Error on line::: TelephonyMgr.listen();

    Multiple markers at this line
    - Syntax error on token(s), misplaced
    construct(s)
    - Syntax error on token(s), misplaced
    construct(s)
    - Syntax error on token ")", invalid
    VariableDeclarator
    - Syntax error on token "(", = expected

    why?

    ReplyDelete
  2. Hi,,

    Capture current any screen of android device when net is on and save as a JPG/ PNG on cloud or webserver.

    Is this possible
    Give me any idea about

    ReplyDelete
  3. how many times the toast in CALL_STATE_OFFHOOK is executed?? is it is once or it will be executing untill end of the call(i.e. the duration of call)

    ReplyDelete
  4. how many time the toast in CALL_STATE_OFFHOOK is called ??? is it is once or will it be called with respective number(number of seconds in call duration)??

    ReplyDelete
  5. Hi, Thanks, awesome article there!
    One doubt though, Is there any method to get callbacks for Conference calls? This method doesn't give callback neither when a call is added in conference nor when it is removed. Thanks

    ReplyDelete