How to handle incoming calls in Android : All framework versions

This post demonstrate how to simply handle incoming call event in Android.

To handle incoming call in an android application you need to do the following :

1. Add the permission use android.permission.READ_PHONE_STATE to your application manifest. This enables the application to have permission to listen to the phone state change event.
2. Create your own PhoneStateListener class like this :

PhoneStateListener myCallListener = new PhoneStateListener(){
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
    // the variable incomingNumber holds the number calling.
    // the state variable holds the state of the phone (Ringing, Idle ...)
      switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
          // your logic here, right now the incoming caller is logged.
          Log.e("Incoming_call", incomingNumber+" is calling me ...");
        break;
        default:
        break;
    }
    super.onCallStateChanged(state, incomingNumber);
   }
};

3. Get an instance of TelephonyManager to use :
TelephonyManager phoneManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
4. Register your PhoneListner object using the TelephoneManager :
     phoneManager.listen(myCallListener, PhoneStateListener.LISTEN_CALL_STATE);

Advertisements
Categories Android, JavaTags , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close