[Android] Telephony, calls

In this lesson, you will learn to intercept incoming calls.

For this, you will learn to use the telephony manager TelephonyManager.

You have to use the telephony manager with getSystemService.

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

Register a listener PhoneStateListener.

MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

Manage the events (ringing, hang up, …) in the method onCallStateChanged().

public class MyPhoneStateListener extends PhoneStateListener {
  public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);
    switch(state) {
      case TelephonyManager.CALL_STATE_IDLE: break;
      case TelephonyManager.CALL_STATE_RINGING: break;
      case TelephonyManager.CALL_STATE_OFFHOOK: break;
    }
  }
}

When the application is interrupted (onPause() called), unregister the listening with the event LISTEN_NONE.

public void onPause() {
  telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}

When the application is resumed (onResume() called), re-register the listening with the event LISTEN_CALL_STATE.

public void onResume() {
  telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

Layout main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<Button
    android:id="@+id/btnCall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="doClick"
    android:text="Call" />
<TextView
    android:id="@+id/tv_log"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

File Main.java

public class Main extends Activity {
       
  TextView tv_log = null;
  TelephonyManager telephonyManager = null;
  MyPhoneStateListener myPhoneStateListener = null;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv_log = (TextView)findViewById(R.id.tv_log);
    Button btnCall = (Button)findViewById (R.id.btnCall);
    btnCall.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:010101"));
        startActivity(intent);
      }
    });
    telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    myPhoneStateListener = new MyPhoneStateListener();
  }
  public void onResume() {
    super.onResume();
    telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
  }
    
  public void onPause() {
    super.onPause();
    telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE);
  }
  public class MyPhoneStateListener extends PhoneStateListener {
     public void onCallStateChanged(int state, String incomingNumber) {
       super.onCallStateChanged(state, incomingNumber);
       switch(state) {
         case TelephonyManager.CALL_STATE_IDLE:
           tv_log.setText("Call state idle : " + incomingNumber); break;
         case TelephonyManager.CALL_STATE_RINGING:
           tv_log.setText("Call state ringing : " + incomingNumber); break;
         case TelephonyManager.CALL_STATE_OFFHOOK:
           tv_log.setText("Call state Offhook : " + incomingNumber); break;
         default:
           tv_log.setText("Call state other : " + incomingNumber); break;
       }
     }
   }
}

File AndroidManifest.xml

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

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×